package;

import flixel.FlxG;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.FlxSprite;
import flixel.FlxSubState;


class UpgradeWindow extends FlxSubState {

    var bg:FlxSprite;
    var titleText:FlxText;
    var buttonLeft:FlxButton;
    var buttonRight:FlxButton;

    var upgrade1:Int = -1;
    var upgrade2:Int = -1;

    static public var upgradeAmounts:Array<Int> = [3, 3, 3, 3, 3, 3, 3];
    // 0: shooting rate
    // 1: projectile damage
    // 2: projectile speed
    // 3: movement speed
    // 4: max health
    // 5: summon area
    // 6: summon regen



    public function new() {
        super();

        bg = new FlxSprite(0, 0, "assets/images/upgrade_window.png");
        bg.camera = PlayState.cameraHUD;
        add(bg);

        titleText = new FlxText(0, 190, FlxG.width, "Choose Upgrade", 24);
		titleText.alignment = FlxTextAlign.CENTER;
		titleText.font = "assets/data/PressStart2P.ttf";
		titleText.camera = PlayState.cameraHUD;
		add(titleText);

        var count:Int = 10;
        upgrade1 = FlxG.random.int(0, 6);
        while (count > 0) {
            if (upgradeAmounts[upgrade1] <= 0) {
                upgrade1 = FlxG.random.int(0, 6);
            }
            else break;
            count--;
        }
        if (upgradeAmounts[upgrade1] <= 0) {  // still coundln't find one
            for (i in 0...7) {
                if (upgradeAmounts[i] > 0) {
                    upgrade1 = i;
                    break;
                }
            }
        }
        if (upgradeAmounts[upgrade1] <= 0) {  // there isn't any upgrade left
            close();
        }

        buttonLeft = new FlxButton(335, 234, "", onButtonLeft);
        buttonLeft.label.text = getUpgradeName(upgrade1);
        buttonLeft.label.font = "assets/data/PressStart2P.ttf";
        buttonLeft.label.size = 12;
        buttonLeft.label.alignment = FlxTextAlign.CENTER;
        //buttonLeft.label.camera = PlayState.cameraHUD;
        buttonLeft.label.offset.y = -12;
        buttonLeft.label.color = 0xffffffff;
        buttonLeft.loadGraphic("assets/images/upgrade_button.png", true, 153, 96);
        buttonLeft.camera = PlayState.cameraHUD;
        add(buttonLeft);


        count = 10;
        upgrade2 = FlxG.random.int(0, 6);
        while (count > 0) {
            if (upgradeAmounts[upgrade2] <= 0 || upgrade2 == upgrade1) {
                upgrade2 = FlxG.random.int(0, 6);
            }
            else break;
            count--;
        }
        if (upgradeAmounts[upgrade2] <= 0 || upgrade2 == upgrade1) {  // still coundln't find one
            for (i in 0...7) {
                if (upgradeAmounts[i] > 0) {
                    upgrade2 = i;
                    break;
                }
            }
        }
        if (upgradeAmounts[upgrade2] <= 0 || upgrade2 == upgrade1) {  // there isn't any upgrade left
            buttonLeft.x = 427;
            buttonLeft.update(FlxG.elapsed);
        }
        else {
            buttonRight = new FlxButton(520, 234, "", onButtonRight);
            buttonRight.label.text = getUpgradeName(upgrade2);
            buttonRight.label.font = "assets/data/PressStart2P.ttf";
            buttonRight.label.size = 12;
            buttonRight.label.alignment = FlxTextAlign.CENTER;
            buttonRight.label.offset.y = -12;
            buttonRight.label.color = 0xffffffff;
            //buttonRight.label.camera = PlayState.cameraHUD;
            buttonRight.loadGraphic("assets/images/upgrade_button.png", true, 153, 96);
            buttonRight.camera = PlayState.cameraHUD;
            add(buttonRight);
        }
    }

    override public function update(elapsed:Float) {
        super.update(elapsed);

        
    }

    function onButtonLeft() {
        upgrade(upgrade1);
        upgradeAmounts[upgrade1]--;
        FlxG.sound.play("assets/sounds/powerup_3.wav", 0.22);
        close();
    }

    function onButtonRight() {
        upgrade(upgrade2);
        upgradeAmounts[upgrade2]--;
        FlxG.sound.play("assets/sounds/powerup_3.wav", 0.22);
        close();
    }

    function upgrade(upgrade:Int) {
        switch (upgrade) {
            case 0:
                Reg.playstate.player.shootTimeMax *= 0.61;
            case 1:
                PlayerBullet.damageStatic *= 1.62;
            case 2:
                PlayerBullet.speed *= 1.23;
            case 3:
                Reg.playstate.player.maxSpeed *= 1.3;
                Reg.playstate.player.maxVelocity.x = Reg.playstate.player.maxSpeed;
                Reg.playstate.player.maxVelocity.y = Reg.playstate.player.maxSpeed;
                Reg.playstate.player.drag.x = Reg.playstate.player.maxSpeed * 3;
                Reg.playstate.player.drag.y = Reg.playstate.player.maxSpeed * 3;
            case 4:
                //Reg.playstate.player.maxHealth *= 1.64;
                Reg.playstate.player.health = Reg.playstate.player.maxHealth;
            case 5:
                Reg.playstate.player.resurrectAreaSize++;
                Reg.playstate.player.resurrectAreaSize++;
                if (Reg.playstate.player.resurrectAreaSize > 6) Reg.playstate.player.resurrectAreaSize = 6;
                Reg.playstate.player.resurrectArea.loadGraphic("assets/images/summon_area_" + Reg.playstate.player.resurrectAreaSize + ".png");
            case 6:
                Reg.playstate.player.canResurrectTime *= 0.64;
                if (Reg.playstate.player.canResurrect > Reg.playstate.player.canResurrectTime) {
                    Reg.playstate.player.canResurrect = Reg.playstate.player.canResurrectTime;
                }
                Reg.playstate.resurrectBar.setRange(0, Reg.playstate.player.canResurrectTime);
        }
    }

    function getUpgradeName(upgrade:Int):String {
        switch (upgrade) {
            case 0: return "Increase\n\nShooting\n\nRate";
            case 1: return "Higher\n\nProjectile\n\nDamage";
            case 2: return "Faster\n\nProjectile\n\nSpeed";
            case 3: return "Increase\n\nMovement\n\nSpeed";
            case 4: return "   \nReplenish\n\nHealth";
            case 5: return "Larger\n\nSummon\n\nArea";
            case 6: return "Faster\n\nSummon\n\nRecovery";
            default: return "Unknown";
        }
    }
}