import { MUtils } from "./MUtils";
import { MConst } from "./MConst";
import { MConfigs } from "./MConfigs";
import {getTexture} from "../utils";

export function getBallScore(bulletScore: number, powerScore: number, colorIndex: number): any {
    let getScoreFromRange = ([a, b]) => {
        a = Math.ceil(a * 0.1);
        b = Math.ceil(b * 0.1);
        let n = Math.abs(b - a) + 1;
        n = Math.floor(n / 6);
        const m = [];
        for (let i = 0; i < 5; i++) {
            m.push([a + n * i, a + n * i + n - 1]);
        }
        m.push([a + n * 4 + n, b])
        const [a0, b0] = m[colorIndex]; //1-10
        let result = Math.ceil(Math.random() * (b0 - a0 + 1)) + a0
        return result;
    }

    if (bulletScore <= 20)
        return getScoreFromRange([0.01 * powerScore, 0.2 * powerScore]);

    if (bulletScore > 20 && bulletScore <= 40) {
        return getScoreFromRange([0.2 * powerScore, 0.6 * powerScore]);
    }

    let getRange = (list: any[], props: any[]) => {
        return getScoreFromRange(getProp(list, props)[0]);
    }

    if (bulletScore > 40 && bulletScore <= 60) {
        const list = [
            [0.2 * powerScore, 0.6 * powerScore],
            [0.6 * powerScore, 0.8 * powerScore],
            [0.8 * powerScore, 1.5 * powerScore],
        ]
        return getRange(list, [3, 5, 2]);
    }
    if (bulletScore > 60 && bulletScore <= 80) {
        const list = [
            [0.6 * powerScore, 0.8 * powerScore],
            [0.8 * powerScore, 1.5 * powerScore],
            [1.5 * powerScore, 2.2 * powerScore],
        ]
        return getRange(list, [2, 5, 2]);
    }

    if (bulletScore > 80 && bulletScore <= 100) {
        const list = [
            [1.5 * powerScore, 2.2 * powerScore],
            [2.2 * powerScore, 3.2 * powerScore],
            [3.2 * powerScore, 4.2 * powerScore],
        ]
        return getRange(list, [1, 5, 4]);
    }

    if (bulletScore > 100 && bulletScore <= 120) {
        const list = [
            [2.2 * powerScore, 3.2 * powerScore],
            [3.2 * powerScore, 4.2 * powerScore],
            [4.2 * powerScore, 5.2 * powerScore],
        ]
        return getRange(list, [1, 4, 5]);
    }

    const list = [
        [2.2 * powerScore, 3.2 * powerScore],
        [3.2 * powerScore, 4.2 * powerScore],
        [4.2 * powerScore, 5.2 * powerScore],
    ]
    return getRange(list, [1, 3, 6]);
}

export function getProp(items: any[], props: number[], n = 1) {
    let pool = [];
    for (let i in items) {
        for (let j = 0; j < props[i]; j++) {
            pool.push(items[i]);
        }

    }

    const result = [];
    for (let i = 0; i < n; i++) {
        result.push(pool[MUtils.randomInt(0, pool.length)]);
    }
    return result;
}

export function arrayRemove(array: any[], target: any): any[] {
    let newArray = [];
    for (let i = 0; i < array.length; i++) {
        if (array[i] !== target) {
            newArray.push(array[i]);
        }
    }
    return newArray;
}
