import LabelType from "./LabelType";
import StartScene from "./StartScene";
const groups = [
    LabelType.IceCream,
    LabelType.Donut,
    LabelType.Egg,
    LabelType.Candy2, LabelType.Gift2,];
const groupName: any = {};
groupName[LabelType.Candy2] = '糖果';
groupName[LabelType.Donut] = '甜甜圈';
groupName[LabelType.Egg] = '彩蛋';
groupName[LabelType.Gift2] = '礼盒';
groupName[LabelType.IceCream] = '冰淇淋';
let currentLv;
currentLv = groups[0];
let data: any = {};
let that: StartScene;
const MAX = 3;

export const initLevel = ($that: StartScene) => {
    that = $that;
    initProgressBar();
    updateLevelView();

}

export const isLevelPass = () => {
    return getProgress(currentLv) == MAX
}

export const isAllLevelPass = () => {
    return getProgress(groups[groups.length - 1]) == MAX
}

export const addItem = (type) => {
    if (currentLv != type) return;
    if (data[type] == undefined)
        data[type] = 0;
    data[type]++;
    if (data[type] > MAX)
        data[type] = MAX;
    updateLevelView();
}

export const clearLevel = () => {
    data[currentLv] = 0;
    updateLevelView();
}

export const nextLevel = () => {
    const i = groups.indexOf(currentLv);
    currentLv = groups[i + 1];
    updateLevelView();
}

 const updateLevelView = () => {
    hideAllGroup();
    that[currentLv].visible = true;
    that['condition'].text = `本轮需要获得3个${groupName[currentLv]}`
    setProgress(currentLv, getProgress(currentLv));
    updateProgressBar();
}

const getProgress = (type) => {
    if (data[type] == undefined)
        return 0
    return data[type];
}

const setProgress = (type, progress) => {
    hideAllItem(type);
    if (progress == 0) {
        that[`${type}_0_1`].visible = true;//0是背景
        that[`${type}_0_2`].visible = true;
        that[`${type}_0_3`].visible = true;
        that[`${type}_1_1`].visible = false;
        that[`${type}_1_2`].visible = false;
        that[`${type}_1_3`].visible = false;
    }
    if (progress == 1) {
        that[`${type}_0_1`].visible = false;//0是背景
        that[`${type}_0_2`].visible = true;
        that[`${type}_0_3`].visible = true;
        that[`${type}_1_1`].visible = true;
        that[`${type}_1_2`].visible = false;
        that[`${type}_1_3`].visible = false;
    }
    if (progress == 2) {
        that[`${type}_0_1`].visible = false;//0是背景
        that[`${type}_0_2`].visible = false;
        that[`${type}_0_3`].visible = true;
        that[`${type}_1_1`].visible = true;
        that[`${type}_1_2`].visible = true;
        that[`${type}_1_3`].visible = false;
    }
    if (progress == 3) {
        that[`${type}_0_1`].visible = false;//0是背景
        that[`${type}_0_2`].visible = false;
        that[`${type}_0_3`].visible = false;
        that[`${type}_1_1`].visible = true;
        that[`${type}_1_2`].visible = true;
        that[`${type}_1_3`].visible = true;
    }
}

const hideAllGroup = () => {
    groups.forEach(group => {
        that[group].visible = false;
    })
}

const hideAllItem = (type) => {
    const names = getItemNames(type);
    names.forEach(name => that[name].visible = false);
}

const getItemNames = (type) => {
    const list = [];
    for (let i = 1; i <= 3; i++) {
        list.push(`_0_${i}`);
    }
    for (let i = 1; i <= 3; i++) {
        list.push(`_1_${i}`);
    }
    return list.map(item => `${type}${item}`)
}

const initProgressBar = () => {
    const progressfg = that['progressfg'];
    const rect = new egret.Rectangle(0, 0, 558, 60);
    rect.width = 0;
    progressfg.mask = rect;
    progressfg['rect'] = rect;

}

const updateProgressBar = () => {
    const progressfg = that['progressfg'];
    let counter = 0;
    for (const group of groups) {
        counter += getProgress(group);
    }
    const targetWidth = 558 * (counter / groups.length / 3);
    egret.Tween.removeTweens(progressfg.mask);
    egret.Tween.get(progressfg.mask).to({ width: targetWidth }, 200);
}