import { request } from 'utils/fetch';
import { STATIC } from './constant';
import { api } from 'constants/config';

// 素材库组列表
export const getGroupList = async () => {
  const res = await request(api.getTemplateGroup);
  const template = res.map((o) => ({ ...o, type: 0 })); // 0是资源中心的模板库分组记录、1是素材库设计素材分组记录、2是素材库主题资源分组记录,素材库不需要拼接type，因为服务端返回了type
  const material = (await request(api.resourceCategory, undefined, { type: '1,2,3' })) || [];
  return [...template, ...material];
};

// 素材库组新增
export const addGroup = (name, type) => {
  if (type === 0) {
    return request(
      api.addTemplateGroup,
      undefined,
      undefined,
      JSON.stringify({
        name,
      }),
    ).then(({ data }) => data);
  } else {
    return request(
      api.resourceAddCategory,
      undefined,
      undefined,
      JSON.stringify({
        name,
        type,
      }),
    ).then(({ data }) => data);
  }
};

// 素材库组删除
export const deleteGroup = (id, type) => {
  const deleteApi = type === 0 ? api.deleteTemplateGroup : api.resourceDeleteCategory;
  return request(deleteApi, { id });
};

// 素材库组重命名
export const editGroup = ({ groupId, name }, type) => {
  const deleteApi = type === 0 ? api.editTemplateGroup : api.resourceEditCategory;
  return request(deleteApi, { id: groupId }, undefined, JSON.stringify({ name }));
};

// 素材库组排序
export const sortGroup = (userCategoryIds, type) => {
  if (type === 0) {
    return request(
      api.sortTemplateGroup,
      undefined,
      undefined,
      JSON.stringify({ groupIds: userCategoryIds }),
    );
  } else {
    return request(
      api.resourceSortCategory,
      undefined,
      undefined,
      JSON.stringify({ userCategoryIds }),
    );
  }
};

// 根据菜单获取对应的内容列表
export const getItemList = (type?: string, categoryType?: number) => {
  if (type && Object.values(STATIC).includes(type)) {
    let url =
      type === 'static-material'
        ? api.resourceUserStaticMaterical
        : type === 'layer-material'
        ? api.resourceUserLayerMaterial
        : null;
    if (!url) {
      return Promise.reject();
    }
    return request(url, undefined, categoryType ? { type: categoryType } : undefined);
  } else {
    return request(api.myTemplates, undefined, { sort: '-createdAt', pageNum: 1, pageSize: 9999 }).then(
      (result) => {
        console.log(result)
        if (result && result.length) {
          return result.map((item) => ({ ...item, userCategoryId: item.groupId }));
        }
        return [];
      },
    );
  }
};

// 内容进行编辑（目前用于分组）
export const editItem = (type, id, groupId) => {
  const userCategoryId = groupId === null ? 0 : groupId;
  if (Object.values(STATIC).includes(type)) {
    return request(
      api.editMaterualList,
      { type, id },
      undefined,
      JSON.stringify({ userCategoryId }),
    );
  } else {
    return request(api.editTemplate, { id }, undefined, JSON.stringify({ groupId }));
  }
};

// 内容重命名
export const renameItem = (type, id, name) => {
  if (Object.values(STATIC).includes(type)) {
    return request(api.editMaterualList, { type, id }, undefined, JSON.stringify({ name }));
  } else {
    return request(api.editTemplate, { id }, undefined, JSON.stringify({ name }));
  }
};

// 删除一个内容
export const deleteItems: any = (ids, type) => {
  if (Object.values(STATIC).includes(type)) {
    return Promise.all(ids.map((id) => request(api.deleteMaterualList, { type, id })));
  } else {
    return Promise.all(ids.map((id) => request(api.deleteTemplate, { id })));
  }
};

// 素材库上传素材
export const addItem = (type, data) => {
  if (Object.values(STATIC).includes(type)) {
    return request(
      api.addMaterualList,
      { type },
      undefined,
      JSON.stringify({
        ...data,
        userCategoryId: data.userCategoryId === null ? undefined : data.userCategoryId,
        thumb: data.type === 3 ? data.thumb : data.content,
        preview: data.type === 3 ? data.thumb : data.content,
      }),
    );
  }
};

//模板库接口

// 导出模板
export const exportTemplates = (id) => request(api.exportTemplate, { id });

// 模板库创建一个模板
export const addTemplate = (templateId) => request(api.templateAddToMy, { templateId: templateId });

// 创建一个大屏
export const createScreen = (templateId) =>
  request(
    api.screenAdd,
    undefined,
    undefined,
    JSON.stringify({
      templateId,
    }),
  );

// 存储数
export const getStorage = () => request(api.getStorage);
