import { DataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import AppCofig from '@/common/config/AppCofig';
import { VolunteerInfo } from '../inhert/VillageApi';

export class LoginResult extends DataModel<LoginResult> {
  constructor() {
    super(LoginResult, "登录结果");
    this._convertTable = {
      token: { clientSide: 'string', clientSideRequired: true },
      userInfo: { clientSide: 'object', clientSideChildDataModel: UserInfo },
      villageVolunteer: { clientSide: 'object', clientSideChildDataModel: VolunteerInfo },
    };
    this._nameMapperServer = {
      'userinfo': 'userInfo',
    }
    this._beforeSolveServer = (data, self) => {
      if (data.userinfo)
      data.token = (data.userinfo as any).token;
      return data;
    }
    this._afterSolveServer = () => {
      if (!this.userInfo.id)
        this.userInfo.id = this.id;
      if (!this.userInfo.mobile)
        this.userInfo.mobile = this.mobile;
      if (!this.userInfo.nickname)
        this.userInfo.nickname = this.nickname;
      if (!this.userInfo.avatar)
        this.userInfo.avatar = this.avatar;
      if (!this.userInfo.username)
        this.userInfo.username = this.username;
    }
  }
  id = 0;
  username = '';
  nickname = '';
  mobile = '';
  avatar = '';
  bio = '';
  score = null as number|null;
  token = '';
  userId = null as number|null;
  createtime = null as number|null;
  expiretime = null as number|null;
  expiresIn = null as number|null;
  inheritorId = null as number|null;
  loginType = 0;
  userInfo = new UserInfo();
  villageVolunteer : VolunteerInfo|null = null;
}
export class UserInfo extends DataModel<UserInfo> {
  constructor() {
    super(UserInfo, "用户信息");
    this.setNameMapperCase('Camel', 'Snake');
    this._nameMapperServer = {
      'userid': 'userId',
      'openid': 'openId',
    }
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
  }

  id = 0;
  userId = 0;
  mobile = '';
  nickname = '';
  avatar = '';
  username = '';
  regionId = 0;
  openId = '';
  isReviewer = false;
}

export class UserApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  static LOGIN_TYPE_ADMIN = 1;
  static LOGIN_TYPE_USER = 0;

  async loginThird(data?: {
    code: string,
    platform: 'wechat',
    encrypted_data: string,
    iv: string,
    raw_data: string,
    signature: string,
  }) {
    return (await this.post('/village/volunteer/third', '登录', {
      appid: AppCofig.appId,
      ...data,
    }, undefined, LoginResult)).data as LoginResult;
  }
  async login(data: {
    account: string,
    password: string,
  }) {
    return (await this.post('/user/login', '登录', data, undefined, LoginResult)).data as LoginResult;
  }
  async loginAdmin(data: {
    account: string,
    password: string,
  }) {
    return (await this.post('/user/adminLogin', '登录', {
      account: data?.account,
      password: data?.password,
    }, undefined, LoginResult)).data as LoginResult;
  }
  async updatePassword(data: {
    newpassword: string,
    oldpassword: string,
  }) {
    return (await this.post('/content/main_body_user/changepwd', '更新密码', data))
  }
  async getUserInfo() {
    return (await this.post('/content/main_body_user/getMainBodyUser', '获取用户信息', {}, undefined, UserInfo)).data as UserInfo;
  }  
  async updateUserInfo(data: {
    nickname?: string,
    avatar?: string,
    intro?: string,
    password?: string,
  }) {
    return (await this.post('/content/main_body_user/editMainBodyUser', '更新用户信息', data))
  }
  async updateSystemUserInfo(data: {
    username?: string,
    nickname?: string,
    avatar?: string,
    bio?: string,
  }) {
    return (await this.post('/user/profile', '更新用户信息', {
      username: data.username,
      nickname: data?.nickname,
      avatar: data?.avatar,
      bio: data?.bio,
    }))
  }
  async refresh() {
    return (await this.post('/ich/inheritor/refresh', '刷新token', {}, undefined, LoginResult)).data as LoginResult;
  }
  async checkUserAuthed() {
    
  }  

  
}

export default new UserApi();
