import Gamecfg from "../../../../common/gameCfg";
import GameDataCenter from "../../../../data/GameDataCenter";
import { MergeItem } from "../../../../data/model/GridMap/GridMapModel";
import ResSprite from "../../../../frameWork/compment/ResSprite";
import EventMng from "../../../../manager/EventMng";
import { HcType } from "../../../../shared/hc/PtlHcInfo";
import { GridEvent } from "../../../gridMap/GridEvent";
import { I_CubeData } from "../../../gridMap/UECube";


export interface MergeSellData {
    isShow: boolean;
    item: I_CubeData;
}
export default class HomeMergeSellCom {
    private _vm: fairygui.GComponent;
    private sp_item: ResSprite;
    private QualityLoad: fgui.GLoader;
    private SellBtn: fgui.GButton;
    private QualityLabel: fgui.GTextField;
    data: MergeSellData;
    public constructor(vm: fgui.GComponent) {
        this._vm = vm;
        this.sp_item = vm.getChild('IconLoad').node.addComponent(ResSprite);
        this.QualityLoad = vm.getChild('QualityLoad').asLoader;
        this.SellBtn = vm.getChild('SellBtn').asButton;
        this.QualityLabel = vm.getChild('QualityLabel').asTextField;
    }

    public onEnable(): void {
        EventMng.on(GridEvent.HC_MERGE_SELL, this.OnHcMergeSell, this);
        this.SellBtn.onClick(this.OnSellBtnClick, this);
    }


    public onDisable(): void {
        EventMng.off(GridEvent.HC_MERGE_SELL, this.OnHcMergeSell, this);
        this.SellBtn.offClick(this.OnSellBtnClick, this);
    }

    private OnSellBtnClick(): void {
        GameDataCenter.gridMap.sendHcSell(this.data.item.idx);
    }


    private OnHcMergeSell(data: MergeSellData): void {
        this.data = data;
        this._vm.visible = data.isShow;
        if (data.isShow) {
            let quality = 0;
            this.SellBtn.visible = false;
            if (data.item.type == HcType.emitter) {
                let mergePropCfg = Gamecfg.emitterInfo.getItem(data.item.id.toString());
                this.sp_item.setSpriteFrame('gridMap', `mergeProp/${mergePropCfg.icon}`);
                quality = mergePropCfg.quality;
            } else if (data.item.type == HcType.material) {
                let mergePropCfg = Gamecfg.mergePropInfo.getItem(data.item.id.toString());
                this.sp_item.setSpriteFrame('gridMap', `mergeProp/${mergePropCfg.icon}`);
                quality = mergePropCfg.quality;
                this.SellBtn.title = mergePropCfg.price.toString();
                this.SellBtn.visible = true;
            } else if (data.item.type == HcType.equip) {
                let mergePropCfg = Gamecfg.equipInfo.getItem(data.item.id.toString());
                this.sp_item.setSpriteFrame('gridMap', `mergeEquip/${mergePropCfg.icon}`);
                quality = mergePropCfg.quality;
                // this.SellBtn.title = mergePropCfg.price.toString();
                this.SellBtn.visible = true;
            } else if (data.item.type == HcType.user) {

            } else if (data.item.type == HcType.monster) {

            }
            if (quality > 0) {
                this.QualityLoad.url = `ui://Home/Img_zjm_xqpinzhi0${quality - 1}`;
                this.QualityLabel.text = `T${quality}`;
            } else {
                this.QualityLoad.url = ``;
            }

        }
    }
}
