import { 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 },
      isLight: { clientSide: 'boolean' },
    }
    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]
      }
      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 = '';
  isLight = false;
  isLightText = '';
  lightValue = Math.random();
  villageVolunteerId = null as number|null;
  villageId !:number;
  claimReason = '';
  status = '';
  statusText = '';
  createdAt = null as Date|null;
  updatedAt = null as Date|null;
  deleteAt = null as Date|null;
  image = '';
  thumbnail = '';
  images = [] as string[];
  villageName = '';
  title = '';
  volunteerName = '';
}
export class LightVillageApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  async getVillageList(level?: number, region?: number, status?: number, page?: number, pageSize?: number) {
    const res = await this.get<{
      data: any[],
      total: number,
    }>('/village/village/list', '乡源村落列表', {
      history_level: level,
      status,
      region: region,
      page: page,
      pageSize: pageSize,
    });
    return transformArrayDataModel<VillageListItem>(VillageListItem, transformSomeToArray(res.requireData().data), `村落`, true);
  }
  

  async getIpAddress() {
    const res = await this.get<{
      address: string,
      address_detail:{
        adcode: string,
        city: string,
        city_code: number,
        district: string,
        province: string,
        street: string,
        street_number: string,
      },
      point: {
        x: string,
        y: string,
      },
    }>('/village/volunteer/getIpArea', '获取IP地址');
    return res.requireData();
  }
}


export default new LightVillageApi();
