import Config from "../../Config";
import { KindItem } from "../../common/Xys";
import GameDataCenter from "../../data/GameDataCenter";
import { FguiEvent, WindowEvent } from "../../data/const/EventConst";
import { AudioConst, DayTipsKey, HelpType, InGame } from "../../data/const/TypeConst";
import EventMng from "../../manager/EventMng";
import UIBase, { BASE_POP, UIClass } from "./UIBase";
import { PayProductParam } from "../../data/const/TypeConst";
import FguiMgr from "../../frameWork/fgui/FguiMgr";
import IDataModel from "../../frameWork/model/IDataModel";
import { I18n } from "../../utils/I18nUtil";
import { uiCommon } from "../../utils/UICommon";
import { eg } from "../../frameWork/eg";
import AssetsBundleMgr from "../../utils/AssetsBundleMgr";
import { ViewZOrder } from "../../data/const/ViewZOrder";
import { DialogViewView } from "../fgui/Common/DialogView/DialogViewView";
import { ShowTipsView } from "../fgui/Common/ShowTips/ShowTipsView";

/**确定框界面参数 */
export interface DialogParams {
    content: string,// 弹窗文本内容
    title?: string,// 弹窗标题
    cbConfirm?: Function,// 确定按钮的事件
    cbCancel?: Function,// 取消按钮的事件
    txtCancel?: string,// 取消按钮的文本
    txtConfirm?: string,// 确定按钮的文本
    onlyConfirm?: boolean//只有确认键
    costItem?: KindItem//消耗道具
    // needItem?: KindItem//需求道具
    showToggle?: boolean,// 是否显示toggle
    useDlg?: string// 哪个界面调用的弹窗
    dayTipsKey?: DayTipsKey,//今日不再提醒key，不为空则设置
    tipsModel?: IDataModel,//本次登录不再提醒model，不为空则设置
    tipsKey?: string,//本次登录不再提醒key，不为空则设置
    showVideoBtn?: boolean,//显示视频按钮
    videoCb?: Function,//视频按钮回调
    maskCloseOnly?: boolean,//点击遮罩只关闭 不执行cancel
    // videoParam?: {
    //     kid: string,
    //     sendUrl: string,
    //     sendParam: any
    // }
}

/**确定框界面参数 */
export interface DialogJintiaoTipsParams {
    data: PayProductParam,
    needNum: number,
    cbConfirm?: Function,
    cbCancel?: Function,
}

/**购买道具弹窗界面参数 */
export interface BuyItemParams {
    // noAskKey: ShopToolKey
    buyItem: KindItem//需要购买的道具
    costItem: KindItem//消耗道具
    cb: Function//购买回调
}

/**通用输入框参数*/
export interface InputParams {
    content?: string,// 弹窗输入框文本内容
    title?: string,// 弹窗标题
    cbConfirm: Function,// 确定按钮的事件
    txtConfirm?: string,// 确定按钮的文本
    limitLength?: number,// 限制的字符数量
}

export default class UIHelp {
    public static SetLabel(node: cc.Node, value: string | number) {
        if (typeof value === 'number') {
            value = value.toString();
        } else if (value == undefined) {
            value = "";
        }
        // 文本和富文本只能二选一
        if (node.getComponent(cc.RichText)) {
            let defaultColor = node.color.toHEX('#rrggbb');
            node.getComponent(cc.RichText).string = `<color=${defaultColor}>${value}</c>`;
        } else {
            node.getComponent(cc.Label).string = value;
        }
    }

    /**按钮灰化，只有注册click事件，才会真正被禁用 */
    public static SetBtnGrayState(node: cc.Node, isGray) {
        let button = node.getComponent(cc.Button);
        if (!button) {
            return;
        }
        button.interactable = !isGray;
        button.enableAutoGrayEffect = isGray;
    }

    public static IsBtnGray(node: cc.Node) {
        let button = node.getComponent(cc.Button);
        if (!button) {
            return false;
        }
        return !button.interactable;
    }

    // 信息飘字提示
    public static ShowTips(message: string) {
        if (FguiMgr.Instance.isShowing(ShowTipsView)) {
            EventMng.emit(FguiEvent.SHOWTIPS, message)
        }
        else {
            FguiMgr.Instance.openUI(ShowTipsView, ViewZOrder.POP_SYSTEM, null, message);
        }
    }

    public static ShowI18nTips(key: string, ...args) {
        let str = I18n.getI18nText(key, ...args)
        this.ShowTips(str);
    }

    public static ShowHelp(type: HelpType, cb?: Function) {
        // FguiMgr.Instance.openUI(HelpViewView, ViewZorder.HELP, cb, type)
    }

    /**
     * 每日提示整合进dialogview内，如果已经勾选，自动执行cbConfirm
     * dayTipsModel改为dailogModel
     * @param data 
     * @returns 
     */
    public static ShowDialog(data: DialogParams, zIndex?: number) {
        if (FguiMgr.Instance.isShowing(DialogViewView)) {
            return
        }
        if (data.dayTipsKey && GameDataCenter.dialog.checkSetToday(data.dayTipsKey) && data.cbConfirm) {
            data.cbConfirm()
            return
        }
        FguiMgr.Instance.openUI(DialogViewView, zIndex ? zIndex : ViewZOrder.Dialog, null, data);
    }
    // 系统文字弹窗
    public static ShowSystemDialog(data: DialogParams) {
        /*if (FguiMgr.Instance.isShowing(DialogViewView)) {
            return
        }
        FguiMgr.Instance.openUI(DialogViewView, ViewZorder.POP_SYSTEM, null, data);*/
        EventMng.emit(WindowEvent.SHOW_SYS_DIALOG, data);
    }

    public static ShowSideItemTips(item: KindItem) {
        // let cfg = GameDataCenter.item.getItemCfgBase(item);
        // let color = uiCommon.getColorByPz(cfg.pinzhi);
        // UIHelp.ShowSideTips(`[color=${color}]${cfg.name}x${item[2]}[/color]`, item);
    }

    // 信息飘字提示
    public static ShowSideTips(message: string, kindItem?: KindItem) {
        // if (FguiMgr.Instance.isShowing(ShowSideTipsView)) {
        //     EventMng.emit(FguiEvent.SHOWSIDETIPS, { msg: message, kindItem: kindItem })
        // }
        // else {
        //     FguiMgr.Instance.openUI(ShowSideTipsView, ViewZorder.POP_SYSTEM, null, { msg: message, kindItem: kindItem });
        // }
    }

    public static FlyItemParticle(startPos: cc.Vec3, endPos: cc.Vec3, panel: fairygui.GObject, item: KindItem, index: number, cb: Function = null) {
        // let expEffect = eg.poolManager.GetPool("FlyItemParticle").get();
        // if (expEffect) {
        //     // 通过 size 接口判断对象池中是否有空闲的对象
        //     expEffect.active = true;
        //     panel.node.addChild(expEffect);
        //     expEffect.getComponent(FlyItemParticle).ShowItemEffect(startPos, endPos, item, index, cb);
        // } else {
        //     // 如果没有空闲对象，也就是对象池中备用对象不够时
        //     AssetsBundleMgr.loadBundlePrefab("particle/flyItem", (err, prefab: cc.Prefab) => {
        //         if (prefab) {
        //             expEffect = cc.instantiate(prefab);
        //             expEffect.active = true;
        //             expEffect.name = "expEffect";
        //             panel.node.addChild(expEffect);
        //             expEffect.getComponent(FlyItemParticle).ShowItemEffect(startPos, endPos, item, index, cb);
        //         } else {
        //             console.log("空的particle/flyItem资源");
        //         }
        //     });
        // }
    }

    /** 调试弹框用 */
    public static ShowDebugDialog(msg: string) {
        let data: DialogParams = {
            content: msg,
            cbConfirm: () => {
            }
        };
        this.ShowDialog(data);
    }
}