import { DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import { GetContentListItem } from '../CommonContent';

export class UserCollectItem extends DataModel<UserCollectItem> {
  constructor() {
    super(UserCollectItem, "收藏条目");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      createdAt: { clientSide: 'date' },
    }
    this._nameMapperServer = {
    };
  }
  mainBodyUserId = null as number|null;
  contentId = null as number|null;
  groupId = null as number|null;
  createdAt = '';
  title = '';
  image = '';
  flag = '';
  type = '';
  mainBodyColumnId = null as number|null;
  modelId = null as number|null;
  modelName = '';
  columnName = '';
  flagText = '';
  typeText = '';
  thumbnail = '';
}
export class UserCollectGroupItem extends DataModel<UserCollectGroupItem> {
  constructor() {
    super(UserCollectGroupItem, "分组");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {}
    this._nameMapperServer = {
    };
  }
  id = 0;
  name = '';
}


export class UserApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  async addCollectGroup(name: string) {
    return (this.post('/content/main_body_user/saveGroup', {
      name,
      content_id: 0,
    }, '增加收藏分组'));
  }
  async updateCollectGroup(id: number, name: string) {
    return (this.post('/content/main_body_user/recently', {
      id,
      name: 0,
      content_id: 0,
    }, '更新收藏分组'));
  }

  async getUserCollect(page: number, pageSize: number, groupId?: number, modelId?: number, searchValue?: string) {
    return (this.get('/content/main_body_user/getUserCollect', '获取收藏内容列表', {
      group_id: groupId,
      model_id: modelId,
      page,
      pageSize,
      keywords: searchValue,
    }))
      .then(res => ({
        list: transformArrayDataModel<UserCollectItem>(UserCollectItem, Array.from(res.data2.data), `获取收藏内容列表`, true),
        total: res.data2.total as number
      }))
      .catch(e => { throw e });
  }
  async getCollectGroups(page: number, pageSize: number, searchValue: string) {
    return (this.get('/content/main_body_user/getGroup', '获取收藏夹', {
      page,
      pageSize,
      keywords: searchValue,
    }))
      .then(res => transformArrayDataModel<UserCollectGroupItem>(UserCollectGroupItem, Array.from(res.data2), `获取收藏夹`, true))
      .catch(e => { throw e });
  }
  async getUserRecently(page: number, pageSize: number, modelId?: number, searchValue?: string) {
    return (this.get('/content/main_body_user/getUserRecently', '获取最近浏览列表', {
      page,
      pageSize,
      model_id: modelId,
      keywords: searchValue,
    }))
      .then(res => ({
        list: transformArrayDataModel<UserCollectItem>(UserCollectItem, Array.from(res.data2.data), `获取最近浏览列表`, true),
        total: res.data2.total as number
      }))
      .catch(e => { throw e });
  }

  async addDeviceToRecently(id: number, deviceId: number) {
    return (this.get('/content/main_body_user/recently', '最近浏览（监控） ', {
      device_id: deviceId,
      content_id: id,
    }));
  }
  async collectDevice(id: number, deviceId: number, groupId: number) {
    return (this.get('/content/main_body_user/collect', '收藏监控', {
      device_id: deviceId,
      group_id: groupId,
      content_id: id,
    }));
  }
  async unCollectDevice(id: number, deviceId: number) {
    return (this.get('/content/main_body_user/uncollect', '取消收藏监控', {
      device_id: deviceId,
      content_ids: id,
    }));
  }
}

export default new UserApi();
