import { Framework } from "../../framework/Framework";
import { ObjectUtil } from "../../framework/util/ObjectUtil";
import { AwardData, BaseItem, ItemEnum } from "../common/InterfaceAddEnum";
import { MaterialConf } from "../config/MaterialConf";
import { GameEvent } from "../data/GameEvent";
import {GoodsData } from "../data/GoodsData";

export class GoodsManager {
    static getBagGoods() {
        let _goodsData = GoodsData.getAllGoods();
        let bagList = [];
        for (const key in _goodsData) {
            if (Object.prototype.hasOwnProperty.call(_goodsData, key)) {
                const element = _goodsData[key];
                if (element.count > 0 && Number(element.conf['Showable'] == 1)) {
                    bagList.push(element);
                }
            }
        }
        bagList.sort((a, b) => {
            return Number(a.id) > Number(b.id) ? -1 : 1;
        });
        return bagList;
    }

    static getGoodsById(id: string) {
        let _goodsData = GoodsData.getAllGoods();
        if (_goodsData[id]) {
            return _goodsData[id];
        } else {
            let mConf = MaterialConf.data[id];
            if (mConf) {
                let item:BaseItem  = {
                    id: id,
                    count: 0,
                    type: ItemEnum.material,
                    conf: mConf,
                }
                return item;
            }
        }
        return null;
    }

    static addGoodsById(id: string, num: number):BaseItem {
        let _goodsData = GoodsData.getAllGoods();
        let mConf = MaterialConf.data[id];
        if (mConf) {
            
            if (_goodsData[id]) {
                let sumCount = _goodsData[id].count + num;
                if (mConf.MaxStoreLimit) {
                    if (sumCount > mConf.MaxStoreLimit) {
                        _goodsData[id].count = mConf.MaxStoreLimit
                    } else {
                        _goodsData[id].count = sumCount
                    }
                } else {
                    _goodsData[id].count = sumCount
                }

            } else {
                let item:BaseItem = {
                    id: id,
                    count: num,
                    type: ItemEnum.material,
                    conf: mConf,
                }
                _goodsData[id] = item;
            }
            Framework.event.fireEvent(GameEvent.MaterialChange, id);
            //後期可能有紅點邏輯

            let changeItem:BaseItem = ObjectUtil.deepCopy(_goodsData[id])
            changeItem.count = num
            return changeItem
        }
    }

    static getGoodsLocalInfo(id: string, num: number) {
        let _goodsData = GoodsData.getAllGoods();
        let mConf = MaterialConf.data[id];
        if (mConf) {
            let item:BaseItem = {
                id: id,
                count: num,
                type: ItemEnum.material,
                conf: mConf,
            }
            return item;
        }
        return null;
    }

    
    // //消息-讀郵件
    // static sendReadMailMsg(args: { id: string }, callback) {
    //     LoginMgr.sendPost('user', 'read_mail', (data) => {
    //         console.log(data);
    //         // MailData.setMailReadById(args.id)
    //     }, args)
    // }
}