import { plainToInstance } from "class-transformer";
import { VipAuthEntity } from "../entity/VipAuthEntity";
import { apiService } from "../net/ApiService";
import { ToastUtils } from "../utils/ToastUtils";
import { BaseViewModel } from "./BaseViewModel";

@ObservedV2
export class AuthViewModel extends BaseViewModel {
  @Trace authInfo?: VipAuthEntity
  @Trace errorCode: number = 0;

  async checkVip() {
    this.showLoading();
    try {
      const result = await apiService.checkPermission('download');
      if (result.isSuccess()) {
        this.authInfo = plainToInstance(VipAuthEntity, result.data);
      } else {
        this.errorCode = result.code
        ToastUtils.show(result.message, true);
      }
      this.dismissLoading()
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading();
    }
  }
}