import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ArtService {

  constructor(private http: HttpClient) { }

  /**
   * Upload page data.
   * @param data FormData
   */
  upload(data) {
    let headers = new HttpHeaders({
      //'Content-Type': 'multipart/form-data',
      'Accept': 'application/json',
    });
    let options = { headers };
    return this.http.post('/napi/web/art/', data, options);
  }



  /**
   * Update page data.
   * @param data FormData
   */
  updatePage(data) {
    let headers = new HttpHeaders({
      //'Content-Type': 'multipart/form-data',
      'Accept': 'application/json',
    });
    let options = { headers };
    return this.http.patch('/napi/web/art/page', data, options);
  }

  patchMeta(data) {
    return this.http.patch(`/napi/web/art/meta`, data);
  }

  get(id: string) {
    return this.http.get(`/napi/web/art/${id}`)
  }

  // 获取作品的发布上架详情
  getShelfInfo(id: string) {
    return this.http.get(`/napi/web/art/${id}/shelfinfo`);
  }

  getTags() {
    return this.http.get(`/napi/web/art/agg/tags`)
  }

  getEpgs() {
    return this.http.get(`/napi/web/epg/options`)
  }

  getHeaders() {
    return this.http.get(`/napi/web/art/pager/headers`)
  }

  delete(id: string) {
    return this.http.delete(`/napi/web/page/${id}`)
  }

  /**
   * 将作品丢入回收站
   * @param pageId  
   * @param reason 
   */
  drop(pageId, reason) {
    return this.http.patch(`/napi/web/art/${pageId}/drop`, { reason });
  }

  /**
   * 将作品从回收站恢复
   * @param pageId 
   */
  recover(pageId) {
    return this.http.patch(`/napi/web/art/${pageId}/recover`, {});
  }

  // 删除炫彩图
  deleteSpecial(id) {
    return this.http.delete(`/napi/web/art/special/${id}`)
  }

  // 上传炫彩缩略图
  uploadSpecialThumb(data: FormData) {
    return this.http.patch(`/napi/web/art/specialthumb`, data);
  }

  // 删除炫彩缩略图
  deleteSpecialThumb(id) {
    return this.http.delete(`/napi/web/art/specialthumb/${id}`)
  }

  // 改变当前special缩略图
  useSpecialThumb(id: string, value: number) {
    return this.http.patch(`/napi/web/art/${id}/usespecialthumb`, { useSpecialThumb: value });
  }

  // 设置作品为神秘图开关
  setMystery(id: string, value: boolean) {
    return this.http.patch(`/napi/web/art/${id}/mystery`, { mystery: value });
  }

  // 上传AI参考图
  uploadAiImage(data: FormData) {
    return this.http.patch(`/napi/web/art/aiimage`, data);
  }

  // 删除AI参考图
  deleteAiImage(id) {
    return this.http.delete(`/napi/web/art/aiimage/${id}`)
  }

  // 设置作品为AI参考图
  setAi(id: string, value: boolean) {
    return this.http.patch(`/napi/web/art/${id}/ai`, { ai: value });
  }

  // 设置作品的AI prompt
  setAiPrompt(id: string, value: string) {
    return this.http.patch(`/napi/web/art/${id}/aiprompt`, { aiPrompt: value });
  }

  // 更改作品用途
  setUse(id: string, value: string) {
    return this.http.patch(`/napi/web/art/${id}/use`, { use: value });
  }



  getAll(id) {
    return this.http.get(`/napi/web/page/${id}/data`)
  }

  list() {
    return this.http.get(`/napi/web/page/datatable`)
  }

  myList() {
    return this.http.get(`/napi/web/page/my/datatable`)
  }


  headers() {
    return this.http.get(`/napi/web/art/datatable/headers`)
  }

  datatable(params) {
    return this.http.post(`/napi/web/art/datatable`, params)
  }

  myDatatable(params) {
    return this.http.post(`/napi/web/page/my/datatable`, params)
  }

  /**
   * Update status.
   * @param id 
   * @param status 
   */
  status(id, status) {
    return this.http.patch(`/napi/web/art/${id}/status/${status}`, {})
  }

  /**
   * 提交自测
   * @param id 
   * @returns 
   */
  selftest(id) {
    return this.http.patch(`/napi/web/art/${id}/selftest`, {})
  }

  /**
   * 更新锁定
   * @param id 
   * @param lock 
   */
  lock(id: string, lock: boolean) {
    return this.http.patch(`/napi/web/art/${id}/lock`, { lock })
  }

  lockOperation(id: string, lock: boolean) {
    return (() => {
      return this.lock(id, lock);
    }).bind(this)
  }

  /**
   * 拒稿
   * @param id 
   * @param reason 
   */
  refuse(id: string, reason: string) {
    return this.http.patch(`/napi/web/art/${id}/refuse`, { reason })
  }

  /**
   * 更新作品的epg上架计划
   * @param id 
   * @param list 
   * @returns 
   */
  epgSchedule(id: string, list: string[]) {
    return this.http.patch(`/napi/web/art/${id}/epgschedule/`, list)
  }


  /**
   * create work for user.
   * @param page pageID
   * @param user userID
   */
  assignTask(data) {
    return this.http.patch('/napi/web/art/assign', data);
  }


  getMainCategoryOptions(lang = 'zh_CN') {
    return this.http.get(`/napi/number/v5/list/category?lang=${lang}`).pipe(map((data: any[]) => {
      return data.map(item => ({ label: item.title, value: item.tag })).filter(item => {
        return !['latest', 'data_good', 'recommend'].includes(item.value);
      })
    }));
  }



  //////////////////////publish-schedule-api//////////////
  createPublishSchedule(data: any) {
    return this.http.post(`/napi/web/publish-schedule`, data);
  }

  getPublishSchedule(id: string) {
    return this.http.get(`/napi/web/publish-schedule/${id}`);
  }

  updatePublishSchedule(id: string, data: any) {
    return this.http.patch(`/napi/web/publish-schedule/${id}`, data);
  }

  deletePublishSchedule(id: string) {
    return this.http.delete(`/napi/web/publish-schedule/${id}`);
  }

  publishScheduleInsert(id: string, data: any) {
    return this.http.patch(`/napi/web/publish-schedule/insert/${id}`, data);
  }

  publishScheduleReorder(id: string, data: any) {
    return this.http.patch(`/napi/web/publish-schedule/reorder/${id}`, data);
  }

  /**
   * 从定时发布中剔除一个作品
   * @param id 
   * @param data 
   * @returns 
   */
  publishScheduleRemove(id: string, data: any) {
    return this.http.patch(`/napi/web/publish-schedule/remove/${id}`, data);
  }



  //////////////////////// score 评分 ///////////////////////////
  /**
   * 获取某个art作品的评分详情
   * @param id 
   * @returns 
   */
  getScoreDetail(id) {
    return this.http.get(`/napi/coloring/web/score/detail/${id}`);
  }

  /**
   * 获取我对某个art作品的评分
   * @param id 
   * @returns 
   */
  getMyScoreFor(id) {
    return this.http.get(`/napi/coloring/web/score/art/${id}`);
  }


  /**
   * 提交对某个作品的评分
   * @param data 
   * @returns 
   */
  postScore(data) {
    return this.http.post(`/napi/coloring/web/score`, data);
  }

  myScoreTasks() {
    return this.http.get(`/napi/coloring/web/score/task/my?page=1&length=30&order=desc&orderBy=time&filters={}`)
  }



}
