import { DataModel, transformArrayDataModel, type NewDataModel } from '@imengyu/js-request-transform';
import { MapServerRequestModule } from '../RequestModules';
import { type QueryParams } from '@imengyu/imengyu-utils';

export class Poi extends DataModel<Poi> {
  constructor() {
    super(Poi, "Poi");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      location: { clientSide: 'splitCommaArray' },
    };
    this._convertKeyType = (key, direction) => {
      if (key.endsWith('Time'))
        return {
          clientSide: 'date',
          serverSide: 'string',
        };
      return undefined;
    };
  }
  name = '';
  location = [] as number[];
  type = '';
  address = '';
  cityname = '';
}

export class MapApi extends MapServerRequestModule<DataModel> {

  constructor() {
    super();
  }


  searchPoi(name: string, page = 1, size = 10, querys?: QueryParams) {
    return this.get(`/v3/place/text?keywords=${encodeURIComponent(name)}&citylimit=true&offset=${size}&page=${page}`, `搜索POI ${name} 第${page}页`, {
      ...querys,
    })
      .then(res => transformArrayDataModel(Poi, res.data ?? [], ''))
      .catch(e => { throw e });
  }
  regeo(lat: number, lng: number, querys?: QueryParams) {
    return this.get(`/v3/geocode/regeo`, `查询经纬度(${lat}, ${lng})所属区县`, {
      location: `${lng},${lat}`,
      ...querys,
    })
      .then(res => (res.data as any).regeocode.addressComponent as {
        country: string,
        province: string,
        city: string,
        district: string,
        adcode: string,
        township: string,
        citycode: string,
        towncode: string,
      })
      .catch(e => { throw e });
  }
  regeoAddress(lat: number, lng: number, querys?: QueryParams) {
    return this.get(`/v3/geocode/regeo`, `查询经纬度(${lat}, ${lng})地址`, {
      location: `${lng},${lat}`,
      ...querys,
    })
      .then(res => (res.data as any).regeocode.formattedAddress as string)
      .catch(e => { throw e });
  }
  loadCityData() {
    return new Promise((resolve, reject) => {
      uni.request({
        url: 'https://mn.wenlvti.net/app_static/xiangyuan/data/ChinaCityData.slim.json',
        method: 'GET',
        success(result) {
          if (result.statusCode === 200) {
            resolve(result.data);
          } else {
            reject(new Error(`请求失败，状态码：${result.statusCode}`));
          }
        },
        fail(error) {
          reject(error);
        }
      })
    });
  }
  loadCountryGeoJsonData() {
    return new Promise((resolve, reject) => {
      uni.request({
        url: 'https://mn.wenlvti.net/app_static/xiangyuan/data/100000_full.json',
        method: 'GET',
        success(result) {
          if (result.statusCode === 200) {
            resolve(result.data);
          } else {
            reject(new Error(`请求失败，状态码：${result.statusCode}`));
          }
        },
        fail(error) {
          reject(error);
        }
      })
    });
  }
  loadGeoJsonData(code: string) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: `https://mn.wenlvti.net/app_static/xiangyuan/data/${code}_full.json`,
        method: 'GET',
        success(result) {
          if (result.statusCode === 200) {
            resolve(result.data);
          } else {
            reject(new Error(`请求失败，状态码：${result.statusCode}`));
          }
        },
        fail(error) {
          reject(error);
        }
      })
    });
  }
}

export default new MapApi();