import { Framework } from "../../framework/Framework";
import { ItemEnum } from "../common/InterfaceAddEnum";
import { MaterialConf } from "../config/MaterialConf";
import { GameEvent } from "./GameEvent";

interface BaseItem {
    conf: {};
    id: string;
    count: number;
    type: ItemEnum;
}

class Data {
    private _goodsData: { [id: string]: BaseItem } = {};

    init(): void {
        this.reset();
    }

    reset(): void {
        this._goodsData = {};

    }

    purge(): void {
        this.reset();
    }

    setAllGoods(data) {
        this._goodsData = {};
        let conf = MaterialConf.data;
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                const element = data[key];
                let mConf = conf[key];
                if (mConf) {
                    let item = {
                        id: key,
                        count: Number(element),
                        type: ItemEnum.material,
                        conf: mConf,
                    }
                    this._goodsData[key] = item;
                }
            }
        }
    }

    getAllGoods() {
        return this._goodsData;
    }

    getBagGoods() {
        let bagList = [];
        for (const key in this._goodsData) {
            if (Object.prototype.hasOwnProperty.call(this._goodsData, key)) {
                const element = this._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;
    }

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

    addGoodsById(id: string, num: number) {
        let mConf = MaterialConf.data[id];
        if (mConf) {
            if (this._goodsData[id]) {
                let sumCount = this._goodsData[id].count + num;
                if (mConf.MaxStoreLimit) {
                    if (sumCount > mConf.MaxStoreLimit) {
                        this._goodsData[id].count = mConf.MaxStoreLimit
                    } else {
                        this._goodsData[id].count = sumCount
                    }
                } else {
                    this._goodsData[id].count = sumCount
                }
            } else {
                let item = {
                    id: id,
                    count: num,
                    type: ItemEnum.material,
                    conf: mConf,
                }
                this._goodsData[id] = item;
            }
            Framework.event.fireEvent(GameEvent.MaterialChange, id);
            //後期可能有紅點邏輯
        }
    }

    getGoodsLocalInfo(id: string, num: number) {
        let mConf = MaterialConf.data[id];
        if (mConf) {
            let item = {
                id: id,
                count: 0,
                type: num,
                conf: mConf,
            }
            return item;
        }
        return null;
    }
}

export let GoodsData = new Data;