import { gameMethod } from "../../common/gameMethod";
import { KindItem, OrderList, SevBack } from "../../common/Xys";
import Config from "../../Config";
import { PayProductParam, ConstItem, PlatFormDevType, DayTipsKey } from "../const/TypeConst";
import GameDataCenter from "../GameDataCenter";
import UIHelp, { DialogJintiaoTipsParams, DialogParams } from "../../logic/ui/UIHelp";
import { I18n } from "../../utils/I18nUtil";
import FguiMgr from "../../frameWork/fgui/FguiMgr";
import { DialogViewView } from "../../logic/fgui/Common/DialogView/DialogViewView";
import IDataModel from "../../frameWork/model/IDataModel";

const { ccclass, property } = cc._decorator;

export default class OrderModel extends IDataModel {
    constructor() {
        super('order');
    }
    doEvent(result: SevBack): void {

    }

    //免费,钻石,代金券,支付,广告统一调用接口
    // dcData.need 空数组 免费, 1货币&普通道具, 10充值(包含代金券支付), 11广告
    commonSendCharge(kid: string, orderList: OrderList, dcData: { title: string, need: KindItem }, sendUrl: string, sendParam: any, cb?: (isSucc: boolean, sevBack?: SevBack) => void, count: number = 1) {
        //请求对应的下单接口    
        if (dcData?.need?.[0] == 10 && dcData?.need?.[1] > 0 && kid != "actShopJinTiao" && GameDataCenter.item.getItemCount(ConstItem.voucher) >= dcData?.need?.[1]) {
            if (GameDataCenter.dialog.checkSetToday(DayTipsKey.chargeByDaiJinQuan)) {
                //勾选了今日不再提醒，直接使用代金券
                sendParam["daibi"] = 1;
                this.SendCharge(kid, orderList, dcData, sendUrl, sendParam, cb, count);
            } else {
                //代金券足够并且不是直接购买代金券的商品，弹出二次确认框是否使用代金券
                let costItemInfo = GameDataCenter.item.getItemCfgBase(ConstItem.voucher);
                let costItem: KindItem = [];
                costItem[0] = ConstItem.voucher?.[0] ?? 1;
                costItem[1] = ConstItem.voucher?.[1];
                costItem[2] = orderList.daibi * count;
                let data: DialogParams = {
                    content: I18n.getI18nText('dialogJintiaoTipsView_txtContent', costItemInfo.name),
                    txtCancel: this.isCanCharge(kid) ? I18n.getI18nText('dialogJintiaoTipsView_txt_zf') : I18n.getI18nText('buyDjq', costItemInfo.name),
                    costItem: costItem,
                    showToggle: true,
                    dayTipsKey: DayTipsKey.chargeByDaiJinQuan,
                    maskCloseOnly: true,
                    cbConfirm: () => {
                        sendParam["daibi"] = 1;
                        this.SendCharge(kid, orderList, dcData, sendUrl, sendParam, cb, count);
                    },
                    cbCancel: () => {
                        if (this.isCanCharge(kid)) {
                            sendParam["daibi"] = 0;
                            this.SendCharge(kid, orderList, dcData, sendUrl, sendParam, cb, count);
                        }
                        else {
                            GameDataCenter.page.openShopVoucher();
                        }
                    }
                }
                FguiMgr.Instance.openUI(DialogViewView, ViewZorder.POP, null, data);
            }
        } else {
            this.SendCharge(kid, orderList, dcData, sendUrl, sendParam, cb, count);
        }
    }

    //发送支付
    SendCharge(kid: string, orderList: OrderList, dcData: { title: string, need: KindItem }, sendUrl: string, sendParam: any, cb?: (isSucc: boolean, sevBack?: SevBack) => void, count: number = 1) {
        this.send(sendUrl, sendParam, (result: SevBack) => {
            if (result.type == 1) {
                if (!gameMethod.isEmpty(result.order10Id)) {
                    if (!this.canPay) {
                        console.log("后台配置关闭支付了")
                        return
                    }
                    if (gameMethod.isEmpty(dcData?.title)) {
                        //如果没有传商品标题，给个默认值防止充值失败，服务端必须要传！！！！！
                        dcData.title = "默认礼包"
                    }
                    console.log(`支付kid:${kid},orderList:${JSON.stringify(orderList)},dcData:${JSON.stringify(dcData)}`)
                    //特殊平台不能支付,需要走代金券
                    if (this.isCanCharge(kid)) {
                        //有下单的id,拉取平台支付
                        let data: PayProductParam = {
                            price: orderList.rmb * count,
                            productId: orderList.iid,
                            productName: dcData.title,
                            productDesc: orderList.desc
                        }
                        this.showPlatPay(result.order10Id, result.order10cs, data)
                    } else {
                        //消耗代金券,弹窗提示
                        console.log("不能直接购买，需要购买代金券")
                        let itemCfg = GameDataCenter.item.getItemCfgBase(ConstItem.voucher);
                        let data: DialogParams = {
                            content: I18n.getI18nText("buyDjqTips", itemCfg.name, itemCfg.name),
                            title: I18n.getI18nText("common_dialogtitle_1"),
                            cbConfirm: () => {
                                GameDataCenter.page.openShopVoucher();
                            }
                        }
                        FguiMgr.Instance.openUI(DialogViewView, ViewZorder.POP, null, data);

                    }

                } else if (!gameMethod.isEmpty(result.order11Id)) {
                    //广告
                    GameDataCenter.adVideo.sendSeeVideo(kid, result.order11Id, result.order11cs, cb)
                } else {
                    //货币&普通道具
                    if (cb) {
                        cb(true, result);
                    }
                }
            }
        })
    }

    isCanCharge(kid: string): boolean {
        if (!gameMethod.isEmpty(GameDataCenter?.login.playerInfo?.switch?.djqPay) && kid != "actShopJinTiao") {
            //17wan、微信小游戏的安卓不能直接支付
            return false;
        }
        return true
    }

    showPlatPay(order10Id: string, order10cs: string, data: PayProductParam) {
        console.log("掉起支付接口")
        GameDataCenter.plat.instance.pay(order10Id, order10cs, data)
    }

    // 是否可以支付 (正常支付或下单直接领奖)
    get canPay(): boolean {
        // 无配置,走正常支付
        if (gameMethod.isEmpty(GameDataCenter.login.playerInfo?.switch?.openPays?.[Config.pid])) {
            return true
        }
        return GameDataCenter.login.playerInfo?.switch?.openPays[Config.pid] == 2
    }

    //ios是否开启支付
    get isOpenPay(): boolean {
        if (GameDataCenter.plat.instance.deviceOS == PlatFormDevType.ios || GameDataCenter.plat.instance.deviceOS == PlatFormDevType.mac) {
            //审核包关闭ios支付及所有入口
            if (gameMethod.isEmpty(GameDataCenter?.login.playerInfo?.switch?.iosPay)) {
                return false
            }
        }
        return true
    }
}