import { CONVERTER_ADD_DEFAULT, DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import { transformSomeToArray } from '@/common/request/utils/Utils';

export class VillageListItem extends DataModel<VillageListItem> {
  constructor() {
    super(VillageListItem, "活动列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
    this._nameMapperServer = {
      name: 'villageName',
    };
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('At'))
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };
    this._afterSolveServer = () => {
      this.address = 
        (this.province || '未知省') + 
        (this.city || '未知市') + 
        (this.district || '未知区') + 
        (this.township || '未知村');
      if (this.images && this.images && this.images.length > 0  ) {
        this.image = this.images[0]
      }
    }

  }

  id !: number;
  province = '' as string|null;
  city = '' as string|null;
  district = '' as string|null;
  township = '' as string|null;
  address = '';
  villageVolunteerId = null as number|null;
  villageId = null as number|null;
  claimReason = '';
  status = '';
  statusText = '';
  createdAt = null as Date|null;
  updatedAt = null as Date|null;
  deleteAt = null as Date|null;
  image = '';
  images = [] as string[];
  villageName = '';
  volunteerName = '';
}
export class VolunteerRanklistItem extends DataModel<VolunteerRanklistItem> {
  constructor() {
    super(VolunteerRanklistItem, "活动列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      points: [{ clientSide: 'number', serverSide: 'number' }, { clientSide: CONVERTER_ADD_DEFAULT, clientSideParam: { defaultValue: 0 } }],
      level: [{ clientSide: 'number', serverSide: 'number' }, { clientSide: CONVERTER_ADD_DEFAULT, clientSideParam: { defaultValue: 0 } }],
    }
  }

  id !: number;
  name = '';
  mobile = '';
  points = 0;
  level = 0;
  typeText = '';
  sexText = '';
  statusText = '';
  image = '';
}
export class VolunteerInfo extends DataModel<VolunteerInfo> {
  constructor() {
    super(VolunteerInfo, "活动列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      points: [{ clientSide: 'number', serverSide: 'number' }, { clientSide: CONVERTER_ADD_DEFAULT, clientSideParam: { defaultValue: 0 } }],
      level: [{ clientSide: 'number', serverSide: 'number' }, { clientSide: CONVERTER_ADD_DEFAULT, clientSideParam: { defaultValue: 0 } }],
    }
  }

  id !: number;
  mainBodyId !: number;
  type = '';
  name = '';
  sex = 0;
  mobile = '';
  regionId = null as number|null;
  address = '';
  image = '';
  birthday = new Date();
  intro = '';
  points = 0;
  level = 0;
  status = '';
  createdAt = '';
  updatedAt = '';
  typeText = '';
  sexText = '';
  statusText = '';
}

export class VillageMenuListItem extends DataModel<VillageMenuListItem> {
  constructor() {
    super(VillageMenuListItem, "村落菜单列表");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
    this._nameMapperServer = {
    };
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('At'))
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };

  }
  name = '';
  logo = '';
}

export class VillageApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  async getClaimedVallageList(volunteerId?: string) {
    return (this.get('/village/village/getVillageList', '获取已认领村落', {
      village_volunteer_id: volunteerId,
    })) 
      .then(res => transformArrayDataModel<VillageListItem>(VillageListItem, res.data2, `村落`, true))
      .catch(e => { throw e });
  }
  async getCanClaimVallageList() {
    return (this.get('/village/village/getClaimList', '可认领村落列表', {
      main_body_id: 2,
    })) 
      .then(res => transformArrayDataModel<VillageListItem>(VillageListItem, transformSomeToArray(res.data2), `村落`, true))
      .catch(e => { throw e });
  }
  async claimVallage(data: any) {
    return (this.post('/village/village/addVillageClaim', {
      ...data
    }, '认领村落')) ;
  }
  async getVallageList() {
    return (this.get('/village/village/getList', '村落列表', {
    })) 
      .then(res => transformArrayDataModel<VillageListItem>(VillageListItem, transformSomeToArray(res.data2), `村落`, true))
      .catch(e => { throw e });
  }

  async getVolunteerInfo() {
    return (await this.post('/village/volunteer/getInfo', {
    }, '获取志愿者信息', undefined, VolunteerInfo)).data as VolunteerInfo
  }
  async getVolunteerRanklist(category?: number) {
    return (this.post('/village/volunteer/getRanklist', {
      category,
    }, '志愿者排行榜')) 
      .then(res => transformArrayDataModel<VolunteerRanklistItem>(VolunteerRanklistItem, res.data2, ``, true))
      .catch(e => { throw e });
  }
  
  async getVillageMenuList(id: number) {
    return (this.get('/village/menu/getList', '村落菜单列表', {
      village_id: id,
      platform: 2,
    })) 
      .then(res => transformArrayDataModel<VillageMenuListItem>(VillageMenuListItem, res.data2, `村落菜单`, true))
      .catch(e => { throw e });
  }

}

export default new VillageApi();
