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, Animation.AnimationListener {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    private Animation fadeIn, fadeOut;
    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();
        fadeIn = AnimationUtils.loadAnimation(game.getContext(), R.anim.fade_in);
        fadeIn.setAnimationListener(this);
        fadeOut = AnimationUtils.loadAnimation(game.getContext(), R.anim.fade_out);

        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), context, button1));
        buttons.add(new ToolButton(toolSlotSettings.get(1), context, button2));
        buttons.add(new ToolButton(toolSlotSettings.get(2), context, button3));
        buttons.add(new ToolButton(toolSlotSettings.get(3), context, button4));
    }

    public void setTopPrimary() {
        button1.startAnimation(fadeIn);
        button2.startAnimation(fadeIn);
        button3.startAnimation(fadeIn);
        button4.startAnimation(fadeIn);
    }

    public void setBottomPrimary() {
        button1.startAnimation(fadeOut);
        button2.startAnimation(fadeOut);
        button3.startAnimation(fadeOut);
        button4.startAnimation(fadeOut);
    }

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

    public void reset(ToolSlotSettings toolSlotSettings) {
        GameLog.i("Reset toolbuttonBar");
        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) {
            ToolType clickedType = null;
            if (v.equals(button1) && !buttons.get(0).isLocked()) {
                clickedType = buttons.get(0).getToolType();
            } else if (v.equals(button2) && !buttons.get(1).isLocked()) {
                clickedType = buttons.get(1).getToolType();
            } else if (v.equals(button3) && !buttons.get(2).isLocked()) {
                clickedType = buttons.get(2).getToolType();
            } else if (v.equals(button4) && !buttons.get(3).isLocked()) {
                clickedType = buttons.get(3).getToolType();
            }
            if (clickedType != null) {
                game.setCurrentTool(clickedType);
                setActive(clickedType);
            }
        }
    }
}
