import { plainToInstance } from "class-transformer";
import { DiamondDetailEntity } from "../entity/DiamondDetailEntity";
import { OrderEntity } from "../entity/OrderEntity";
import { PayOrderEntity } from "../entity/OrderPayEntity";
import { VipMealEntity } from "../entity/VipMealEntity";
import { apiService } from "../net/ApiService";
import { ToastUtils } from "../utils/ToastUtils";
import { BaseViewModel } from "./BaseViewModel";

@ObservedV2
export class DiamondViewModel extends BaseViewModel {
  @Trace diamondInfo?: DiamondDetailEntity
  @Trace goodsList?: Array<VipMealEntity>;
  @Trace payOrderEntity?: PayOrderEntity;
  @Trace orderInfoEntity?: OrderEntity;

  private intervalId = 0

  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();
    }
  }

  async mealList() {
    this.showLoading();
    try {
      const result = await apiService.goodsList('recharge');
      if (result.isSuccess()) {
        this.goodsList = plainToInstance(VipMealEntity, result.data as Array<Object>);
      } else {
        ToastUtils.show(result.message, true);
      }
      this.dismissLoading();
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading();
    }
  }

  async createOrder(goodsId: string, payType: string, source: string, coupon: string){
    this.showLoading();
    try {
      const result = await apiService.createOrder(goodsId, payType, source, coupon);
      if(result.isSuccess()) {
        this.payOrderEntity = plainToInstance(PayOrderEntity, result.data);
      } else {
        ToastUtils.show(result.message, true);
      }
      this.dismissLoading();
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
      this.dismissLoading();
    }
  }

  async getOrderInfo(orderId: string) {
    this.showLoading(true)
    this.cancelInterval()
    try {
      let count = 10;
      this.intervalId = setInterval(async () => {
        if (count > 0) {
          count--;
          const result = await apiService.getOrderInfo(orderId);
          if (result.isSuccess()) {
            this.orderInfoEntity = plainToInstance(OrderEntity, result.data);
            if (this.orderInfoEntity.status == '2') {
              this.dismissLoading()
              this.cancelInterval()
            }
          } else {
            ToastUtils.show(result.message, true);
            this.dismissLoading()
            this.cancelInterval()
          }
        } else {
          this.dismissLoading()
          this.cancelInterval()
        }
      }, 2000)
    } catch (e) {
      console.log(e);
      this.dismissLoading()
      this.cancelInterval()
    }
  }

  cancelInterval() {
    if (this.intervalId !== 0) {
      clearInterval(this.intervalId)
    }
  }
}