import { Injectable } from '@angular/core';
import { EImage } from 'picsur-shared/dist/entities/image.entity';
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
import { ApiService } from './api.service';
import { ImageUploadRequest } from '../models/image-upload-request';

@Injectable({
  providedIn: 'root',
})
export class ImageService {
  constructor(private api: ApiService) {}

  public async UploadImage(image: File): AsyncFailable<string> {
    const result = await this.api.postForm(
      EImage,
      '/i',
      new ImageUploadRequest(image)
    );

    if (HasFailed(result)) return result;

    return result.hash;
  }

  public async GetImageMeta(image: string): AsyncFailable<EImage> {
    return await this.api.get(EImage, `/i/meta/${image}`);
  }

  public GetImageURL(image: string): string {
    const baseURL = window.location.protocol + '//' + window.location.host;

    return `${baseURL}/i/${image}`;
  }

  public CreateImageLinks(imageURL: string) {
    return {
      source: imageURL,
      markdown: `![image](${imageURL})`,
      html: `<img src="${imageURL}" alt="image">`,
      rst: `.. image:: ${imageURL}`,
      bbcode: `[img]${imageURL}[/img]`,
    };
  }
}
