import { plainToInstance } from 'class-transformer';
import { VipPermissionEntity } from '../entity/VipPermissionEntity';
import { WxVideoEntity } from '../entity/WxVideoEntity';
import { WxVideoServiceEntity } from '../entity/WxVideoServiceEntity';
import { apiService } from '../net/ApiService';
import { ToastUtils } from '../utils/ToastUtils';
import { BaseViewModel } from './BaseViewModel';

@ObservedV2
export class WxVideoViewModel extends BaseViewModel {
  @Trace wxVideo?: WxVideoEntity;
  @Trace deleteVideo?: object;
  @Trace permissionInfo?: VipPermissionEntity;
  @Trace errorCode: number = 0;
  @Trace wxService?: WxVideoServiceEntity
  @Trace wxUserinfo?: object

  async videoList() {
    this.showLoading()
    try {
      const result = await apiService.wxVideoList()
      if (result.isSuccess()) {
        this.wxVideo = plainToInstance(WxVideoEntity, result.data);
      } else {
        this.errorCode = result.code
        ToastUtils.show(result.message, true)
      }
      this.dismissLoading()
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading()
    }
  }

  async deleteWxVideo(logId: string) {
    this.showLoading()
    try {
      const result = await apiService.deleteWxVideo(logId)
      if (result.isSuccess()) {
        this.deleteVideo = new Object()
      } else {
        ToastUtils.show(result.message, true)
      }
      this.dismissLoading()
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading()
    }
  }

  async reportStatus(logId: string, status: string, size: string = '', message: string = '') {
    try {
      apiService.reportDownLoadStatus(logId, status, size, message)
    } catch (e) {
      console.log(e);
    }
  }

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

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

  async bindWxUserinfo(code: string) {
    this.showLoading()
    try {
      const result = await apiService.bindWxUserInfo(code)
      if (result.isSuccess()) {
        this.wxUserinfo = new Object()
      } else {
        ToastUtils.show(result.message, true)
      }
      this.dismissLoading()
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading();
    }
  }
}