package project.scenes;

import pi.Controller;
import pi.Rectangle;
import pi.Scene;
import pi.Text;
import pi.actor.Actor;
import pi.actor.Animation;
import pi.actor.Image;
import pi.config.ConfigLoader;
import pi.event.FrameListener;
import pi.event.MouseButton;
import pi.event.MouseClickListener;
import pi.graphics.geom.Vector;
import project.Main;
import project.utils.Statistics;

import static pi.Controller.fonts;
import java.awt.Font;


public class GameScene extends Scene implements FrameListener, MouseClickListener
{
    Rectangle bg;
    Image tatli;
    Text[] texts;
    Image[] ingredients;  // array of all ingredients (including the final kebab lol)
    static int INGREDIENTS_X = 13;
    static int INGREDIENTS_X_TRESHOLD = 7;
    static int INGREDIENTS_Y = -25;
    
    int[] current_kebab;  // used for remembering which ingredients have already been added to the current kebab build
    Actor[] customers;
    boolean[] fed_customers;
    public static ConfigLoader loader;
    boolean mouse_pressed_on_kebab;
    Statistics s;

    public GameScene() {
        // the background music, color, stats (some meta information)
        Controller.jukebox.playSound("sounds/Mozart_Turkish_March_-_Orchestral_Melodic_Techno_-_TVV_music_320k.mp3");

        // background and stuff
        bg = new Rectangle(2000, 2000);
        bg.color("#ffffff");
        bg.center(0, 0);
        bg.layerPosition(-100);
        addStatsCollectionOutline(this);
        texts = createTexts(this);
        camera().meter(10);
        s = Main.CONFIG.getGroup(Statistics.class);

        tatli = new Image("images/kebab-pack/tatli-ekmek-256x256.png");
        tatli.size(2.56*13, 2.56*13);
        tatli.center(-33.4, -23);
        
        // create customers
        customers = new Actor[2];
        customers[0] = Animation.createFromAnimatedGif("images/kebab-pack/customer-sonic-on-white.gif", 1.48*25, 1.25*25);
        customers[0].center(-60,0);
        customers[0].applyImpulse(new Vector(1, 0).multiply(200000));
        customers[1] = Animation.createFromAnimatedGif("images/kebab-pack/customer-mr-eggman.gif", 1.48*20, 1.48*20);
        customers[1].center(-180,0);
        customers[1].applyImpulse(new Vector(1, 0).multiply(100000));
        fed_customers = new boolean[customers.length];

        // initialize ingredients and current_kebab arrays
        ingredients = createIngredients();
        current_kebab = new int[ingredients.length-1];

        mouse_pressed_on_kebab = false;

        // add all actors
        add(bg);
        add(customers);
        add(ingredients);
        add(tatli);
    }

    @Override
    public void onFrame(double pastTime)
    {
        for(int i=0; i<customers.length; i++) {
            if(visibleArea().xRight() < customers[i].anchor().add(new Vector(-20, 0)).x()) {
                customers[i].center(-60,0);
                if(fed_customers[i]) {
                    fed_customers[i] = false;   
                } else {
                    incrementLostCustomers(1);
                }
            }
        }
        
        if(mouse_pressed_on_kebab) {
            ingredients[ingredients.length-1].center(Controller.mousePosition());
        }
    }

    @Override
    public void onMouseDown(Vector position, MouseButton button)
    {
        if(ingredients[ingredients.length-1].contains(Controller.mousePosition()) && ingredients[ingredients.length-1].isVisible()) {
            mouse_pressed_on_kebab = true;
        }

        for(int i=0; i<4; i++) {
            if (ingredients[i].contains(position)) {
                current_kebab[i] += 1;
            }
        }

        boolean everything_added = true;
        for(int i : current_kebab){
            if(i == 0) {
                everything_added = false;
            }
        }
        if(everything_added) {
            for(int i=0; i<current_kebab.length; i++) {
                current_kebab[i] = 0;
            }
            ingredients[ingredients.length-1].visible(true);
        }
    }

    @Override
    public void onMouseUp(Vector position, MouseButton button) {
        for (int i=0; i<customers.length; i++) {
            // only sell kebab when (1) a kebab is (2) dragged on a customer that has (3) not already been fed
            if(mouse_pressed_on_kebab && customers[i].contains(position) && !fed_customers[i]) {
                sellKebabStatistics(1);
                ingredients[ingredients.length-1].visible(false);
                ingredients[ingredients.length-1].center(INGREDIENTS_X + INGREDIENTS_X_TRESHOLD*(ingredients.length-1) +2, INGREDIENTS_Y);
                fed_customers[i] = true;
            }
        }

        // must be at bottom for previous checks to work
        mouse_pressed_on_kebab = false;
    }
    
    // Static helper methods
    public static Image[] createIngredients() {
        Image[] ingredients = new Image[5];

        ingredients[0] = new Image("images/kebab-pack/onion-256x256.png");
        ingredients[1] = new Image("images/kebab-pack/meat-256x256.png");
        ingredients[2] = new Image("images/kebab-pack/bread-256x256.png");
        ingredients[3] = new Image("images/kebab-pack/lettuce-96x96.png");
        ingredients[4] = new Image("images/kebab-pack/kebab-128x128.png");
        ingredients[4].visible(false);

        for (int i=0; i<ingredients.length-1; i++) {
            ingredients[i].size(5, 5).center(INGREDIENTS_X + INGREDIENTS_X_TRESHOLD*i, INGREDIENTS_Y);
        }

        ingredients[ingredients.length-1].size(7, 7).center(INGREDIENTS_X + INGREDIENTS_X_TRESHOLD*(ingredients.length-1) +2, INGREDIENTS_Y);

        return ingredients;
    }
    
    public static void addStatsCollectionOutline(Scene scene) {
        Image moneyButton = new Image("images/pixui-pack/Buttons/Button_White_Outline (9).png");
        moneyButton.center(-10, 25);
        Image kebabSoldButton = new Image("images/pixui-pack/Buttons/Button_White_Outline (9).png");
        kebabSoldButton.center(0, 25);
        Image lostCustomersButton = new Image("images/pixui-pack/Buttons/Button_White_Outline (9).png");
        lostCustomersButton.center(10, 25);

        Image moneyIcon = new Image("images/kebab-pack/money-128x128.png").size(2, 2);
        moneyIcon.center(-12, 25);
        Image kebabSoldIcon = new Image("images/kebab-pack/kebab-128x128.png").size(2, 2);
        kebabSoldIcon.center(-2, 25);
        Image lostCustomersIcon = new Image("images/kebab-pack/customer.png").size(2, 2);
        lostCustomersIcon.center(8, 25);
        
        scene.add(moneyButton);
        scene.add(moneyIcon);
        scene.add(kebabSoldButton);
        scene.add(kebabSoldIcon);
        scene.add(lostCustomersButton);
        scene.add(lostCustomersIcon);
    }

    public static Text[] createTexts(Scene scene) {
        Text[] texts = new Text[10];
        Font font = fonts.get("fonts/BitcountGridDouble-Regular.ttf");

        Statistics s = Main.CONFIG.getGroup(Statistics.class);
        
        Text money = new Text(s.money());
        money.color("#000000");
        money.font(font);
        money.height(2.5);
        money.center(-9, 25);
        texts[0] = money;
        Text kebabSold = new Text(s.kebabSold());
        kebabSold.color("#000000");
        kebabSold.font(font);
        kebabSold.height(2.5);
        kebabSold.center(1, 25);
        texts[1] = kebabSold;
        Text lostCustomers = new Text(s.lostCustomers());
        lostCustomers.color("#000000");
        lostCustomers.font(font);
        lostCustomers.height(2.5);
        lostCustomers.center(11, 25);
        texts[2] = lostCustomers;

        scene.add(money);
        scene.add(kebabSold);
        scene.add(lostCustomers);

        return texts;
    }

    public static void setTexts(Text[] texts, int money, int kebabSold, int lostCustomers) {
        texts[0].content(String.valueOf(money));
        texts[1].content(String.valueOf(kebabSold));
        texts[2].content(String.valueOf(lostCustomers));
    }

    public static void setMoney(Text[] texts, int money) {
        texts[0].content(String.valueOf(money));
    }
    public static void setKebabSold(Text[] texts, int kebabSold) {
        texts[1].content(String.valueOf(kebabSold));
    }
    public static void setLostCustomers(Text[] texts, int lostCustomers) {
        texts[2].content(String.valueOf(lostCustomers));
    }
    public void sellKebabStatistics(int amount) {
        s.kebabSold(s.kebabSold()+amount);
        s.money(s.money() + s.kebabPrice());
        Main.CONFIG.save();
        setKebabSold(texts, s.kebabSold());
        setMoney(texts, s.money());

        if(s.money() >= 67) {
            resetStats();
            Controller.transitionToScene(new VictoryScene());
        }
    }
    public void incrementLostCustomers(int amount) {
        s.lostCustomers(s.lostCustomers()+amount);
        Main.CONFIG.save();
        setLostCustomers(texts, s.lostCustomers());
    }

    public static void saveStats(Text[] texts) {
        int money = Integer.parseInt(texts[0].content());
        int kebabSold = Integer.parseInt(texts[1].content());
        int lostCustomers = Integer.parseInt(texts[2].content());

        System.out.println(money);
        System.out.println(kebabSold);
        System.out.println(lostCustomers);
    }
    public void resetStats() {
        s.money(2);
        s.kebabPrice(5);
        s.kebabSold(0);
        s.lostCustomers(0);
        Main.CONFIG.save();
    }
}