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' },
          }
        } 
      },
    }
    this._beforeSolveServer = (self) => {
      if (!self.mainBodyUserInfo && self.userinfo) {
        self.mainBodyUserInfo = {
          ...(self.userinfo as any),
        }
      }
      if (!self.auth && self.userinfo) {
        self.auth = {
          ...(self.userinfo as any),
        }
      }
      return self;
    }
  }
  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' },
      adminGroup: {
        customToClientFn: (data: any) => {
          const adminGroup = [] as { id: number, name: string }[];
          for (const key in data) {
            if (data[key])
              adminGroup.push({ 
              id: data[key].group_id as number, 
              name: data[key].name as string 
            });
          }
          console.log(data, adminGroup);
          return adminGroup;
        },
      },
    }
  }

  id = 0;
  userId = 0;
  mainBodyId = 0;
  adminGroup = [] as { id: number, name: string }[];
  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 updateUserInfo(data: {
    nickname?: string,
    avatar?: string,
    intro?: string,
    password?: string,
  }) {
    return (await this.post('/content/main_body_user/editMainBodyUser', '更新用户信息', data))
  }
  async updatePassword(data: {
    newpassword: string,
    oldpassword: string,
  }) {
    return (await this.post('/content/main_body_user/changepwd', '更新密码', data))
  }
  async refresh() {
    return (await this.post('/content/main_body_user/refreshUser', '刷新用户', {
    }, undefined, LoginResult)).data as LoginResult;
  }
  async checkUserAuthed(id: number) {
    return await this.post('/content/main_body_user/getMainBodyUser', '检查用户是否登录', {
      main_body_user_id: id,
    });
  }  
}

export default new UserApi();
