export default {
    data() {
        return {
            $models: {}, // 天赋源对象
            $cur: { total: 51, used: 0, items: {}, tabPoints: [0, 0, 0] }, // 存储数据,
        };
    },
    methods: {
        // 只会在源数据发生改变才需要重新初始化,如切换职业
        $init() {
            // 可能存在数据覆盖,比如获取的已存储信息
            let ietms = {};
            for (let k in this.$models) {
                ietms[k] = { id: k, cur: 0, state: "000" };
            }
            this.$cur = { total: 51, used: 0, items: ietms, tabPoints: [0, 0, 0] };
            this.$checkState(this.$cur);
            this.$computAfter();
        },
        $add(id, point) {
            point = parseInt(point);
            let tmp = uni.$uv.deepClone(this.$cur);
            let operrb = this.$tryAdd(tmp, id, point, true);
            if (operrb) {
                this.$cur = tmp;
                this.$checkState(this.$cur);
                this.$computAfter();
            }
            return operrb;
        },
        $tryAdd(obj, id, point, canchange) {
            obj.items[id].cur += point;
            obj.used += point;

            const i = parseInt(id.charAt(0));
            obj.tabPoints[i - 1] += point;

            let ruler = this.$checkRule(obj);
            if (!canchange || !ruler) {
                obj.items[id].cur -= point;
                obj.used -= point;
            }
            return ruler;
        },
        $subtract(id, point) {
            point = parseInt(point);
            let tmp = uni.$uv.deepClone(this.$cur);
            let operrb = this.$trySubtract(tmp, id, point, true);
            if (operrb) {
                this.$cur = tmp;
                this.$checkState(this.$cur);
                this.$computAfter();
            }
            return operrb;
        },
        $trySubtract(obj, id, point, canchange) {
            obj.items[id].cur -= point;
            obj.used -= point;
            const i = parseInt(id.charAt(0));
            obj.tabPoints[i - 1] -= point;

            let ruler = this.$checkRule(obj);
            if (!canchange || !ruler) {
                obj.items[id].cur += point;
                obj.used += point;
            }

            return ruler;
        },
        $computAfter() {
            this.$emit("change", this.$cur);
            this.$forceUpdate();
        },
        $clearTab(key) {
            const { items } = this.$cur;
            let tabUsed = 0;
            for (const k in items) {
                const i = parseInt(k.charAt(0));
                if (key == i) {
                    tabUsed += items[k].cur;
                    items[k].cur = 0;
                }
            }
            this.$cur.used = this.$cur.used - tabUsed;
            this.$cur.tabPoints[key - 1] = 0;
            this.$checkState(this.$cur);
            this.$computAfter();
        },
        $checkRule(obj) {
            for (let k in obj.items) {
                let curObj = obj.items[k];
                let curObjStruct = this.$models[k];
                for (let i = 0, s = curObjStruct.rules.length; i < s; i++) {
                    let r = curObjStruct.rules[i];
                    let rarr = r.split("_");
                    let b = this[rarr[0]](this.$models, obj, curObj, rarr[1]);
                    if (!b) {
                        return false;
                    }
                }
            }
            return true;
        },
        $checkState(obj) {
            for (let k in obj.items) {
                // state-000: 不可用不可增不可减; state-110:可用可增不可减；
                // state-101：可用不可增可减；state-111：可用可增可减
                // [false,false,false]:不可用不可增不可减; [[true,true,false]]:可用可增不可减；[true,false,true]：可用不可增可减；[true,true,true]：可用可增可减
                let curObj = obj.items[k];
                let tmp = uni.$uv.deepClone(obj);
                // 判断可增
                let state1 = this.$tryAdd(tmp, k, 1, false);
                // 判断可减
                let state2 = this.$trySubtract(tmp, k, 1, false);
                // 判断可用
                let state0 = state1 || state2;
                // 101
                curObj.state = this.$getStrByState(state0) + this.$getStrByState(state1) + this.$getStrByState(state2);
            }
            return true;
        },
        $getStrByState(state) {
            return state ? "1" : "0";
        },
        // 校验按顺序执行,
        pointEnough(model, obj) {
            if (obj.used < 0 || obj.used > obj.total) {
                return false;
            }
            return true;
        },
        scopeIn(model, obj, curObj) {
            let curObjStruct = model[curObj.id];
            if (curObj.cur < 0 || curObj.cur > curObjStruct.t) {
                return false;
            }
            return true;
        },
        layerFull(model, obj, curObj) {
            if (curObj.cur > 0) {
                let curObjStruct = model[curObj.id];
                let count = 0;
                // 需要带上这个层级的
                for (let k in obj.items) {
                    let tObj = obj.items[k];
                    let oObjStruct = model[k];
                    const tab1 = parseInt(oObjStruct.id.charAt(0));
                    const tab2 = parseInt(curObjStruct.id.charAt(0));
                    if (tab1 == tab2 && oObjStruct.layer < curObjStruct.layer) {
                        count = count + tObj.cur;
                    }
                }
                let needCount = (curObjStruct.layer - 1) * 5;
                if (count < needCount) {
                    return false;
                }
            }
            return true;
        },
        preFull(model, obj, curObj) {
            if (curObj.cur > 0) {
                let curObjStruct = model[curObj.id];
                let preObj = obj.items[curObjStruct.pre];
                let preObjStruct = model[preObj.id];
                if (preObj.cur < preObjStruct.t) {
                    return false;
                }
            }
            return true;
        },
    },
    computed: {
        $computedCur() {
            return function (id) {
                return this.$cur?.items[id]?.cur + "/" + this.$models[id]?.t;
            };
        },
    },
};
