import { DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import ApiCofig from '@/common/config/ApiCofig';
import AppCofig from '@/common/config/AppCofig';


export class LoginResult extends DataModel<LoginResult> {
  constructor() {
    super(LoginResult, "登录结果");
    //this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      mainBodyUserInfo: { clientSide: 'object', clientSideRequired: true, clientSideChildDataModel: UserInfo },
      auth: { 
        clientSide: 'object', 
        clientSideRequired: true, 
        clientSideChildDataModel: {
          nameMapperServer: {
            user_id: 'userId',
            expires_in: 'expiresIn',
          },
          convertTable: {
            token: { clientSide: 'string', clientSideRequired: true },
            userId: { clientSide: 'number' },
            expiresIn: { clientSide: 'number' },
          }
        } 
      },
    }
  }
  auth !: {
    id: number,
    username: string,
    nickname: string,
    mobile: string,
    avatar: string,
    score: number,
    token: string,
    userId: number,
    createtime: Date,
    expiretime: Date,
    expiresIn: number,
  };
  mainBodyUserInfo !:UserInfo;
}
export class UserInfo extends DataModel<UserInfo> {
  constructor() {
    super(UserInfo, "用户信息");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      userId: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      mainBodyId: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      loginTime: { clientSide: 'date', serverSide: 'string' },
    }
  }

  id = 0;
  userId = 0;
  mainBodyId = 0;
  nickname = '';
  avatar = '';
  intro = '';
  fans = '';
  score = '';
  loginTime = null as null|Date;
  diyname = '';
}

export class UserApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }

  async loginThird(data?: {
    code: string,
    platform: 'wechat',
    encrypted_data: string,
    iv: string,
    raw_data: string,
    signature: string,
  }) {
    return (await this.post('/content/main_body_user/third', {
      appid: AppCofig.appId,
      ...data,
    }, '登录', undefined, LoginResult)).data as LoginResult;
  }
  async loginWithMobile(data?: {
    mobile: string,
    password: string,
  }) {
    return (await this.post('/discover/User/login', {
      appid: AppCofig.appId,
      ...data,
    }, '登录', undefined, LoginResult)).data as LoginResult;
  }
  async getUserInfo(main_body_user_id: number) {
    return (await this.post('/content/main_body_user/getMainBodyUser', {
      main_body_user_id,
    }, '获取用户信息', undefined, UserInfo)).data as UserInfo;
  }
  async refresh() {
    return (await this.post('/content/main_body_user/refreshUser', {
    }, '刷新用户', undefined, LoginResult)).data as LoginResult;
  }
}

export default new UserApi();
