const aqua = 0x7fdbff;
const black = 0x111111;
const blue = 0x0074d9;
const fuchsia = 0xf012be;
const green = 0x2ecc40;
const lime = 0x01ff70;
const maroon = 0x85144b;
const navy = 0x001f3f;
const olive = 0x3d9970;
const orange = 0xff851b;
const purple = 0xb10dc9;
const red = 0xff4136;
const teal = 0x39cccc;
const white = 0xffffff;
const yellow = 0xffff0e;
const gray = 0x666666;

//const tints = [aqua, black, blue, fuchsia, green, lime, maroon, navy, olive, orange, purple, red, teal, white, yellow];

const { Between, FloatBetween, RadToDeg } = Phaser.Math;

const CubicIn = Phaser.Math.Easing.Cubic.In;

const { GetRandom } = Phaser.Utils.Array;

const primaryColor = Phaser.Display.Color.ValueToColor(0xebfa06);
const secondaryColor = Phaser.Display.Color.ValueToColor(0xb3fa06);

const tints = [ [0xff0000, 0xff0e0e], [0xff0e0e, 0xe04315], [0xd16e0b, 0xd47618], [0xd0a00b, 0xd4a516], 
                [0xe0cc07, 0xe3d015], [0xfffa01, 0xfff30f], [0xbad807, 0xbfdb14], [0x84cc0b, 0x8bd018],
                [0x5ece0b, 0x66d318], [0x49df07, 0x4fe314], [0x44ff00, 0x4cff0f], [0x33d53c, 0x3bd744],
                [0x24b773, 0x30bd7c], [0x16aca7, 0x24b1ad], [0x0abed5, 0x16b4d9], [0x00c5fe, 0x10c7ff],
                [0x3b87de, 0x478de3], [0x7352cf, 0x7c5cd3], [0xa128ca, 0xad36d1], [0xd10edc, 0xd61ade],
                [0xff00fb, 0xff0dfa], [0xff00fb, 0xff0efa] ];


//fx

class LightsFX
{
    constructor (scene)
    {
        this.scene = scene;

        this.upperLeftPads = [];

        this.upperLeftPads[0] = this.scene.add.image(134, 720, "ulpads", "0").setDepth(1);
        this.upperLeftPads[1] = this.scene.add.image(110, 682, "ulpads", "1").setDepth(1);
        this.upperLeftPads[2] = this.scene.add.image(76, 642, "ulpads", "2").setDepth(1);
        this.upperLeftPads[3] = this.scene.add.image(68, 602, "ulpads", "3").setDepth(1);
        this.upperLeftPads[4] = this.scene.add.image(56, 560, "ulpads", "4").setDepth(1);
        this.upperLeftPads[5] = this.scene.add.image(56, 518, "ulpads", "5").setDepth(1);
        this.upperLeftPads[6] = this.scene.add.image(56, 476, "ulpads", "6").setDepth(1);
        this.upperLeftPads[7] = this.scene.add.image(56, 430, "ulpads", "7").setDepth(1);

        this.upperRightPads = [];

        this.upperRightPads[0] = this.scene.add.image(946, 722, "urpads", "0").setDepth(1);
        this.upperRightPads[1] = this.scene.add.image(968, 684, "urpads", "1").setDepth(1);
        this.upperRightPads[2] = this.scene.add.image(1010, 644, "urpads", "2").setDepth(1);
        this.upperRightPads[3] = this.scene.add.image(1016, 601, "urpads", "3").setDepth(1);
        this.upperRightPads[4] = this.scene.add.image(1026, 559, "urpads", "4").setDepth(1);
        this.upperRightPads[5] = this.scene.add.image(1028, 517, "urpads", "5").setDepth(1);
        this.upperRightPads[6] = this.scene.add.image(1026, 473, "urpads", "6").setDepth(1);
        this.upperRightPads[7] = this.scene.add.image(1036, 429, "urpads", "7").setDepth(1);

        this.lowerLeftPads = [];

        this.lowerLeftPads[0] = this.scene.add.image(129, 1219, "lpads", "0").setDepth(1).setFlipX(true).setScale(1, 1.40);
        this.lowerLeftPads[1] = this.scene.add.image(128, 1186, "lpads", "1").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-2);
        this.lowerLeftPads[2] = this.scene.add.image(126, 1149, "lpads", "2").setDepth().setFlipX(true).setScale(1, 1.40).setAngle(-4);
        this.lowerLeftPads[3] = this.scene.add.image(122, 1110, "lpads", "3").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-5);
        this.lowerLeftPads[4] = this.scene.add.image(118, 1070, "lpads", "4").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-7);
        this.lowerLeftPads[5] = this.scene.add.image(113, 1025, "lpads", "5").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-9);
        this.lowerLeftPads[6] = this.scene.add.image(106, 981, "lpads", "6").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-11);
        this.lowerLeftPads[7] = this.scene.add.image(98, 935, "lpads", "7").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-14);
        this.lowerLeftPads[8] = this.scene.add.image(88, 882, "lpads", "8").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-15);
        this.lowerLeftPads[9] = this.scene.add.image(76, 831, "lpads", "9").setDepth(1).setFlipX(true).setScale(1, 1.40).setAngle(-18);

        this.lowerRightPads = [];

        this.lowerRightPads[0] = this.scene.add.image(129 + 821, 1221, "lpads", "0").setDepth(1).setScale(1, 1.40);
        this.lowerRightPads[1] = this.scene.add.image(128 + 824, 1186, "lpads", "1").setDepth(1).setScale(1, 1.40).setAngle(2);
        this.lowerRightPads[2] = this.scene.add.image(126 + 829, 1149, "lpads", "2").setDepth(1).setScale(1, 1.40).setAngle(3);
        this.lowerRightPads[3] = this.scene.add.image(122 + 836, 1110, "lpads", "3").setDepth(1).setScale(1, 1.40).setAngle(4);
        this.lowerRightPads[4] = this.scene.add.image(116 + 847, 1070, "lpads", "4").setDepth(1).setScale(1, 1.40).setAngle(6);
        this.lowerRightPads[5] = this.scene.add.image(113 + 856, 1026, "lpads", "5").setDepth(1).setScale(1, 1.40).setAngle(8);
        this.lowerRightPads[6] = this.scene.add.image(106 + 870, 981, "lpads", "6").setDepth(1).setScale(1, 1.40).setAngle(10);
        this.lowerRightPads[7] = this.scene.add.image(98 + 885, 935, "lpads", "7").setDepth(1).setScale(1, 1.40).setAngle(12);
        this.lowerRightPads[8] = this.scene.add.image(88 + 905, 882, "lpads", "8").setDepth(1).setScale(1, 1.40).setAngle(14);
        this.lowerRightPads[9] = this.scene.add.image(76 + 928, 831, "lpads", "9").setDepth(1).setScale(1, 1.40).setAngle(16);

        this.upperLightsLeft = [];

        this.upperLightsLeft[0] = this.scene.add.image(118, 720, "ulights", "0").setAngle(-11).setDepth(1);
        this.upperLightsLeft[1] = this.scene.add.image(68, 686, "ulights", "1").setAngle(-9).setDepth(1);
        this.upperLightsLeft[2] = this.scene.add.image(68, 642,"ulights", "2").setAngle(-7).setDepth(1);
        this.upperLightsLeft[3] = this.scene.add.image(56, 602, "ulights", "3").setAngle(-6).setDepth(1);
        this.upperLightsLeft[4] = this.scene.add.image(56, 560, "ulights", "4").setAngle(-5).setDepth(1);
        this.upperLightsLeft[5] = this.scene.add.image(46, 518, "ulights", "5").setAngle(-2).setDepth(1);
        this.upperLightsLeft[6] = this.scene.add.image(46, 476, "ulights", "6").setAngle(0).setDepth(1);
        this.upperLightsLeft[7] = this.scene.add.image(46, 430, "ulights", "7").setAngle(0).setDepth(1);

        this.upperLightsRight = [];

        this.upperLightsRight[0] = this.scene.add.image(960, 720, "ulights", "0").setAngle(11).setDepth(1);
        this.upperLightsRight[1] = this.scene.add.image(1010, 688, "ulights", "1").setAngle(10).setDepth(1);
        this.upperLightsRight[2] = this.scene.add.image(1010, 644, "ulights", "2").setAngle(9).setDepth(1);
        this.upperLightsRight[3] = this.scene.add.image(1024, 604, "ulights", "3").setAngle(7).setDepth(1);
        this.upperLightsRight[4] = this.scene.add.image(1024, 559, "ulights", "4").setAngle(6).setDepth(1);
        this.upperLightsRight[5] = this.scene.add.image(1036, 518, "ulights", "5").setAngle(4).setDepth(1);
        this.upperLightsRight[6] = this.scene.add.image(1036, 473, "ulights", "6").setAngle(2).setDepth(1);
        this.upperLightsRight[7] = this.scene.add.image(1036, 429, "ulights", "7").setAngle(0).setDepth(1);
//
        this.lowerLightsLeft = [];

        this.lowerLightsLeft[0] = this.scene.add.image(131, 1221, "dlights", "0").setDepth(1);
        this.lowerLightsLeft[1] = this.scene.add.image(128, 1186, "dlights", "1").setAngle(-1).setDepth(1);
        this.lowerLightsLeft[2] = this.scene.add.image(126, 1150, "dlights", "2").setAngle(-2).setDepth(1);
        this.lowerLightsLeft[3] = this.scene.add.image(123, 1112, "dlights", "3").setAngle(-5).setDepth(1);
        this.lowerLightsLeft[4] = this.scene.add.image(117, 1072, "dlights", "4").setAngle(-6).setDepth(1);
        this.lowerLightsLeft[5] = this.scene.add.image(110, 1027, "dlights", "5").setAngle(-7).setDepth(1);
        this.lowerLightsLeft[6] = this.scene.add.image(106, 982, "dlights", "6").setAngle(-10).setDepth(1);
        this.lowerLightsLeft[7] = this.scene.add.image(96, 936, "dlights", "7").setAngle(-12).setDepth(1);
        this.lowerLightsLeft[8] = this.scene.add.image(86, 884, "dlights", "8").setAngle(-14).setDepth(1);
        this.lowerLightsLeft[9] = this.scene.add.image(74, 832, "dlights", "9").setAngle(-16).setDepth(1);

        this.lowerLightsRight = [];

        this.lowerLightsRight[0] = this.scene.add.image(950, 1221, "dlights", "0").setDepth(1);
        this.lowerLightsRight[1] = this.scene.add.image(953, 1186, "dlights", "1").setAngle(1).setDepth(1);
        this.lowerLightsRight[2] = this.scene.add.image(955, 1150, "dlights", "2").setAngle(2).setDepth(1);
        this.lowerLightsRight[3] = this.scene.add.image(958, 1112, "dlights", "3").setAngle(5).setDepth(1);
        this.lowerLightsRight[4] = this.scene.add.image(962, 1072, "dlights", "4").setAngle(8).setDepth(1);
        this.lowerLightsRight[5] = this.scene.add.image(967, 1027, "dlights", "5").setAngle(9).setDepth(1);
        this.lowerLightsRight[6] = this.scene.add.image(974, 982, "dlights", "6").setAngle(11).setDepth(1);
        this.lowerLightsRight[7] = this.scene.add.image(982, 936, "dlights", "7").setAngle(13).setDepth(1);
        this.lowerLightsRight[8] = this.scene.add.image(991, 883, "dlights", "8").setAngle(14).setDepth(1);
        this.lowerLightsRight[9] = this.scene.add.image(1001, 831, "dlights", "9").setAngle(17).setDepth(1);

        this.centerLights = [];
//top
        this.centerLights[0] = this.scene.add.image(300, 754, "clight").setDepth(3);
        this.centerLights[1] = this.scene.add.image(340, 754, "clight").setDepth(3);
        this.centerLights[2] = this.scene.add.image(380, 754, "clight").setDepth(3);
        this.centerLights[3] = this.scene.add.image(420, 754, "clight").setDepth(3);
        this.centerLights[4] = this.scene.add.image(460, 754, "clight").setDepth(3);
        this.centerLights[5] = this.scene.add.image(500, 754, "clight").setDepth(3);
        this.centerLights[6] = this.scene.add.image(540, 754, "clight").setDepth(3);
        this.centerLights[7] = this.scene.add.image(579, 754, "clight").setDepth(3);
        this.centerLights[8] = this.scene.add.image(619, 754, "clight").setDepth(3);
        this.centerLights[9] = this.scene.add.image(659, 754, "clight").setDepth(3);
        this.centerLights[10] = this.scene.add.image(698, 754, "clight").setDepth(3);
        this.centerLights[11] = this.scene.add.image(738, 754, "clight").setDepth(3);
        this.centerLights[12] = this.scene.add.image(777, 754, "clight").setDepth(3);
//right
        this.centerLights[13] = this.scene.add.image(777, 794, "clight").setDepth(3);
        this.centerLights[14] = this.scene.add.image(777, 834, "clight").setDepth(3);
        this.centerLights[15] = this.scene.add.image(777, 874, "clight").setDepth(3);
        this.centerLights[16] = this.scene.add.image(777, 914, "clight").setDepth(3);
        this.centerLights[17] = this.scene.add.image(777, 954, "clight").setDepth(3);
        this.centerLights[18] = this.scene.add.image(777, 994, "clight").setDepth(3);
        this.centerLights[19] = this.scene.add.image(777, 1033, "clight").setDepth(3);
        this.centerLights[20] = this.scene.add.image(777, 1073, "clight").setDepth(3);
        this.centerLights[21] = this.scene.add.image(777, 1111, "clight").setDepth(3);
        this.centerLights[22] = this.scene.add.image(777, 1150, "clight").setDepth(3);
        this.centerLights[23] = this.scene.add.image(777, 1190, "clight").setDepth(3);
        this.centerLights[24] = this.scene.add.image(777, 1230, "clight").setDepth(3);
//bottom
        this.centerLights[25] = this.scene.add.image(738, 1230, "clight").setDepth(3);
        this.centerLights[26] = this.scene.add.image(698, 1230, "clight").setDepth(3);
        this.centerLights[27] = this.scene.add.image(659, 1230, "clight").setDepth(3);
        this.centerLights[28] = this.scene.add.image(619, 1230, "clight").setDepth(3);
        this.centerLights[29] = this.scene.add.image(579, 1230, "clight").setDepth(3);
        this.centerLights[30] = this.scene.add.image(540, 1230, "clight").setDepth(3);
        this.centerLights[31] = this.scene.add.image(500, 1230, "clight").setDepth(3);
        this.centerLights[32] = this.scene.add.image(460, 1230, "clight").setDepth(3);
        this.centerLights[33] = this.scene.add.image(420, 1230, "clight").setDepth(3);
        this.centerLights[34] = this.scene.add.image(380, 1230, "clight").setDepth(3);
        this.centerLights[35] = this.scene.add.image(340, 1230, "clight").setDepth(3);
        this.centerLights[36] = this.scene.add.image(300, 1230, "clight").setDepth(3);
//left        
        this.centerLights[37] = this.scene.add.image(300, 1190, "clight").setDepth(3);
        this.centerLights[38] = this.scene.add.image(300, 1150, "clight").setDepth(3);
        this.centerLights[39] = this.scene.add.image(300, 1111, "clight").setDepth(3);
        this.centerLights[40] = this.scene.add.image(300, 1073, "clight").setDepth(3);
        this.centerLights[41] = this.scene.add.image(300, 1033, "clight").setDepth(3);
        this.centerLights[42] = this.scene.add.image(300, 994, "clight").setDepth(3);        
        this.centerLights[43] = this.scene.add.image(300, 954, "clight").setDepth(3);
        this.centerLights[44] = this.scene.add.image(300, 914, "clight").setDepth(3);
        this.centerLights[45] = this.scene.add.image(300, 874, "clight").setDepth(3);
        this.centerLights[46] = this.scene.add.image(300, 834, "clight").setDepth(3);
        this.centerLights[47] = this.scene.add.image(300, 794, "clight").setDepth(3);

        this.tintpos = 0;

        this.primaryColor = Phaser.Display.Color.ValueToColor(0x000000);
        this.secondaryColor = Phaser.Display.Color.ValueToColor(0x000000);
    }

    addTint (imgarr, color)
    {
        imgarr.forEach(img => {img.setTint(color);});
    }

    setTexture (imgarr, texture)
    {
        imgarr.forEach(img => {img.setTexture(texture);});
    }

    clearTint (imgarr, tween = undefined)
    {
        imgarr.forEach(img => {img.clearTint(); img.preFX.clear()});

        if (tween != undefined)
        {
            this.scene.tweens.remove(tween);
        }
    }

    clearAllTints ()
    {
        this.upperLeftPads.forEach(img => {img.clearTint();});

        this.upperRightPads.forEach(img => {img.clearTint();});

        this.lowerLeftPads.forEach(img => {img.clearTint();});

        this.lowerRightPads.forEach(img => {img.clearTint();});

        this.upperLightsLeft.forEach(img => {img.clearTint();});

        this.upperLightsRight.forEach(img => {img.clearTint();});

        this.lowerLightsLeft.forEach(img => {img.clearTint();});

        this.lowerLightsRight.forEach(img => {img.clearTint();});

        this.centerLights.forEach(img => {img.clearTint();});
    }

    playFX (fx)
    {
        switch (fx)
        {
            case FX_RUNNING_LIGHTS:this.fxMovingLightsPlay();break;
            case FX_FLASHING_LIGHTS:this.fxFlashingLightsPlay();break;
            case FX_BUST_LIGHTS:this.fxBustLightsPlay();break;
            case FX_COLOR_CHANGE:this.fxColorChangePlay();break;
        }
    }

    stopFX (fx)
    {
        switch (fx)
        {
            case FX_RUNNING_LIGHTS:this.fxMovingLightsStop();break;
            case FX_FLASHING_LIGHTS:this.fxFlashingLightsStop();break;
            case FX_BUST_LIGHTS:this.fxBustLightsStop();break;
            case FX_COLOR_CHANGE:this.fxColorChangeStop();break;
        }
    }

    fxMovingLightsPlay ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            if ((i % 2) == 0)
            {
                this.centerLights[i].setTexture("clightup");
            }
            else
            {
                this.centerLights[i].setTexture("clight");
                this.centerLights[i].setTint(gray);
            }
        }

        this.fxMovingLightsTween = this.scene.tweens.addCounter({
            
            from: 0,
            to: 100,
            duration: 100,
            repeat: -1,

            callbackScope: this,

            onRepeat: function  ()
            {
                for (let i = 0; i < this.centerLights.length; ++i)
                {
                    if (this.centerLights[i].texture.key == "clightup")
                    {
                        this.centerLights[i].setTexture("clight");
                        this.centerLights[i].setTint(gray);
                    }
                    else
                    {
                        this.centerLights[i].setTexture("clightup");
                        this.centerLights[i].clearTint();
                    }
                }
            }
        });
    }

    fxMovingLightsStop ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            this.centerLights[i].setTexture("clight").clearTint();
            this.centerLights[i].preFX.clear();

            if (this.fxMovingLightsTween != undefined)
            {
                this.fxMovingLightsTween.stop();

                this.scene.tweens.remove(this.fxMovingLightsTween);

                this.fxMovingLightsTween = undefined;
            }
        }
    }

    fxFlashingLightsPlay ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            this.centerLights[i].setTexture("clightup");
        }

        this.fxFlashingLightsTween = this.scene.tweens.addCounter({
            
            from: 0,
            to: 100,
            duration: 100,
            repeat: -1,

            callbackScope: this,

            onRepeat: function  ()
            {
                for (let i = 0; i < this.centerLights.length; ++i)
                {
                    if (this.centerLights[i].texture.key == "clightup")
                    {
                        this.centerLights[i].setTexture("clight");
                        this.centerLights[i].setTint(gray);
                    }
                    else
                    {
                        this.centerLights[i].setTexture("clightup");
                        this.centerLights[i].clearTint();
                    }
                }
            }
        });
    }

    fxFlashingLightsStop ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            this.centerLights[i].setTexture("clight").clearTint();

            if (this.fxFlashingLightsTween != undefined)
            {
                this.fxFlashingLightsTween.stop();

                this.scene.tweens.remove(this.fxFlashingLightsTween);

                this.fxFlashingLightsTween = undefined;
            }
        }
    }

    fxBustLightsPlay ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            this.centerLights[i].setTint(gray);
        }
    }

    fxBustLightsStop ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            this.centerLights[i].clearTint();
        }
    }

/*        this.wild = this.add.image(540, 500, "wildbw").setDepth(1);

        let primaryColor = Phaser.Display.Color.ValueToColor(0xce8600);
        let secondaryColor = Phaser.Display.Color.ValueToColor(0x3a116d);

        this.wild.setTint(primaryColor);

        this.tweens.addCounter({
            from: 0,
            to: 1000,
            duration: 2000,
            ease: Phaser.Math.Easing.Sine.InOut,
            repeat: -1,
            yoyo: true,
            onUpdate: tween => {
                
                let value = tween.getValue();
                
                let colorObject = Phaser.Display.Color.Interpolate.ColorWithColor(primaryColor, secondaryColor, 1000, value);

                let color = Phaser.Display.Color.GetColor(colorObject.r, colorObject.g, colorObject.b);

                this.wild.setTint(color);
            }
        })*/

    fxColorChangePlay ()
    {
        this.tintpos = 0;

        this.primaryColor = Phaser.Display.Color.ValueToColor(tints[this.tintpos][0]);
        this.secondaryColor = Phaser.Display.Color.ValueToColor(tints[this.tintpos][1]);

        for (let i = 0; i < this.lowerLightsLeft.length; ++i)
        {
            this.lowerLightsLeft[i].setTint(this.primaryColor);
            this.lowerLeftPads[i].setTint(this.primaryColor);

            this.lowerLightsRight[i].setTint(this.primaryColor);
            this.lowerRightPads[i].setTint(this.primaryColor);
        }

        this.scene.tweens.addCounter({
            from: 0,
            to: 100,
            duration: 500,
            ease: Phaser.Math.Easing.Linear,
            repeat: -1,
            yoyo: true,
            
            callbackScope: this,          
            
            onUpdate: tween => {
                
                let value = tween.getValue();
                
                let colorObject = Phaser.Display.Color.Interpolate.ColorWithColor(this.primaryColor, this.secondaryColor, 100, value);

                let color = Phaser.Display.Color.GetColor(colorObject.r, colorObject.g, colorObject.b);

                for (let i = 0; i < this.lowerLightsLeft.length; ++i)
                {
                    this.lowerLightsLeft[i].setTint(color);
                    this.lowerLeftPads[i].setTint(color);

                    this.lowerLightsRight[i].setTint(color);
                    this.lowerRightPads[i].setTint(color);
                }
            },
            
            onRepeat: function  ()
            {
/*                for (let i = 0; i < this.centerLights.length; ++i)
                {
                    if (this.centerLights[i].texture.key == "clightup")
                    {
                        this.centerLights[i].setTexture("clight");
                        this.centerLights[i].setTint(gray);
                    }
                    else
                    {
                        this.centerLights[i].setTexture("clightup");
                        this.centerLights[i].clearTint();
                    }
                }*/
            }
        })
    }

    fxColorChangeStop ()
    {
        for (let i = 0; i < this.centerLights.length; ++i)
        {
            this.centerLights[i].setTexture("clight").clearTint();

            if (this.fxFlashingLightsTween != undefined)
            {
                this.fxFlashingLightsTween.stop();

                this.scene.tweens.remove(this.fxFlashingLightsTween);

                this.fxFlashingLightsTween = undefined;
            }
        }
    }    
}