import { CONVERTER_ADD_DEFAULT, DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import { transformSomeToArray } from '../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.image && this.images && this.images.length > 0  ) {
        this.image = this.images[0]
      }
      this.thumbnail = this.image;
      this.title = this.villageName
    }

  }

  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 = '';
  title = '';
  status = '';
  statusText = '';
  createdAt = null as Date|null;
  updatedAt = null as Date|null;
  deleteAt = null as Date|null;
  thumbnail = '';
  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) {
    const res = await this.get('/village/village/getVillageList', '获取已认领村落', {
      village_volunteer_id: volunteerId,
    });
    return transformArrayDataModel<VillageListItem>(VillageListItem, res.data as any, `村落`, true);
  }
  async getCanClaimVallageList() {
    const res = await this.get('/village/village/getClaimList', '可认领村落列表', {
      main_body_id: 2,
    });
    return transformArrayDataModel<VillageListItem>(VillageListItem, transformSomeToArray(res.data), `可认领村落`, true);
  }
  async claimVallage(data: any) {
    return (this.post('/village/village/addVillageClaim', '认领村落', {
      ...data
    })) ;
  }
  async getVallageList(level?: number) {
    const res = await this.get('/village/village/getList', '村落列表', {
      history_level: level,
    });
    return transformArrayDataModel<VillageListItem>(VillageListItem, transformSomeToArray(res.data), `村落`, true);
  }

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

}

export default new VillageApi();
