import { plainToInstance } from 'class-transformer';
import { DiamondDetailEntity } from '../entity/DiamondDetailEntity';
import { UserEntity } from '../entity/UserEntity';
import { WxServiceEntity } from '../entity/WxServiceEntity';
import { LoginManager } from '../manager/LoginGlobalManager';
import { apiService } from '../net/ApiService';
import { ToastUtils } from '../utils/ToastUtils';
import { BaseViewModel } from './BaseViewModel';

@ObservedV2
export default class MineViewModel extends BaseViewModel {
  @Trace userEntity?: UserEntity
  @Trace wxService?: WxServiceEntity
  @Trace diamondInfo?: DiamondDetailEntity

  async userinfo() {
    try {
      const result = await apiService.userinfo();
      if (result.isSuccess()) {
        this.userEntity = plainToInstance(UserEntity, result.data);
        LoginManager.setUserInfo(this.userEntity);
      } else {
        ToastUtils.show(result.message, true);
      }
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
    }
  }

  async getWxService() {
    this.showLoading()
    try {
      const result = await apiService.wxService()
      if (result.isSuccess()) {
        this.wxService = plainToInstance(WxServiceEntity, result.data)
      } else {
        ToastUtils.show(result.message, true)
      }
      this.dismissLoading()
    } catch (e) {
      console.log(e)
      ToastUtils.show(e)
      this.dismissLoading()
    }
  }

  async getDiamondInfo() {
    this.showLoading();
    try {
      const result = await apiService.getDiamondInfo();
      if (result.isSuccess()) {
        this.diamondInfo = plainToInstance(DiamondDetailEntity, result.data);
      } else {
        ToastUtils.show(result.message, true);
      }
      this.dismissLoading();
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading();
    }
  }
}