package de.frajul.endlessroll.views;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.main.GameLog;
import de.frajul.endlessroll.main.game.Game;
import de.frajul.endlessroll.main.game.GameState;
import de.frajul.endlessroll.user.ToolSlotSettings;

/**
 * Created by Julian on 16.01.2016.
 */
public class ToolButtonBar implements View.OnClickListener{

    private Game game;
    private List<ToolButton> buttons = new ArrayList<>(4);
    private RelativeLayout button1;
    private RelativeLayout button2;
    private RelativeLayout button3;
    private RelativeLayout button4;


    public ToolButtonBar(Game game, ToolSlotSettings toolSlotSettings, LinearLayout layout1) {
        this.game = game;
        Context context = game.getContext();

        button1 = (RelativeLayout) layout1.findViewById(R.id.toolbutton_1);
        button1.setOnClickListener(this);
        button2 = (RelativeLayout) layout1.findViewById(R.id.toolbutton_2);
        button2.setOnClickListener(this);
        button3 = (RelativeLayout) layout1.findViewById(R.id.toolbutton_3);
        button3.setOnClickListener(this);
        button4 = (RelativeLayout) layout1.findViewById(R.id.toolbutton_4);
        button4.setOnClickListener(this);
        buttons.add(new ToolButton(toolSlotSettings.get(0), game, context, button1));
        buttons.add(new ToolButton(toolSlotSettings.get(1), game, context, button2));
        buttons.add(new ToolButton(toolSlotSettings.get(2), game, context, button3));
        buttons.add(new ToolButton(toolSlotSettings.get(3), game, context, button4));
    }

    public void changeToolButtonTypes(ToolSlotSettings toolSlotSettings) {
        for (int i = 0; i < 4; i++) {
            buttons.get(i).changeToolSlot(toolSlotSettings.get(i));
        }
    }

    public void reset(ToolSlotSettings toolSlotSettings) {
        changeToolButtonTypes(toolSlotSettings);
        for (int i = 0; i < 4; i++) {
            if (toolSlotSettings.get(i).getToolType() != null) {
                setActive(i);
                break;
            }
        }
        for (ToolButton button : buttons)
            button.setProgress(100);
    }

    public void update(float frameTime) {
        for (ToolButton button : buttons)
            button.update(frameTime);
    }

    public void setActive(ToolType activeType) {
        for (ToolButton button : buttons)
            button.setActive(button.getToolType() == activeType);
    }

    private void setActive(int index) {
        for (ToolButton button : buttons)
            button.setActive(buttons.indexOf(button) == index);
    }

    public ToolButton getByToolType(ToolType type) {
        for (ToolButton button : buttons)
            if (button.getToolType() == type)
                return button;
        return null;
    }

    public ToolButton getActiveButton() {
        for (ToolButton button : buttons)
            if (button.isActive())
                return button;
        return null;
    }

    @Override
    public void onClick(View v) {
        if (game.getGameState() == GameState.RUNNING) {
            ToolButton clickedButton = null;
            if (v.equals(button1) && !buttons.get(0).isLocked()) {
                clickedButton = buttons.get(0);
            } else if (v.equals(button2) && !buttons.get(1).isLocked()) {
                clickedButton = buttons.get(1);
            } else if (v.equals(button3) && !buttons.get(2).isLocked()) {
                clickedButton = buttons.get(2);
            } else if (v.equals(button4) && !buttons.get(3).isLocked()) {
                clickedButton = buttons.get(3);
            }
            if (clickedButton != null) {
                game.setCurrentTool(clickedButton.getToolType(), clickedButton.finishedLoading());
                setActive(clickedButton.getToolType());
            }
        }
    }
}
