package de.frajul.endlessroll.views;

import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.entities.shapes.PlayerShape;
import de.frajul.endlessroll.entities.shapes.PlayerShapeButtonOnClickListener;
import de.frajul.endlessroll.main.GameActivity;

/**
 * Created by Julian on 22.10.2017.
 */

public class PlayerShapeButton implements View.OnClickListener {

    private PlayerShape playerShape;
    private PlayerShapeButtonOnClickListener clickListener;

    private View view;
    private TextView textView;
    private Button button;
    private Animation rotation;

    private boolean locked;

    public PlayerShapeButton(GameActivity gameActivity, PlayerShape playerShape, PlayerShapeButtonOnClickListener clickListener, boolean locked, boolean marginToRight) {
        this.clickListener = clickListener;
        this.playerShape = playerShape;
        this.locked = locked;
        view = LayoutInflater.from(gameActivity).inflate(R.layout.shape_button, null);
        view.setLayoutParams(createLayoutParams(marginToRight));
        textView = (TextView) view.findViewById(R.id.shape_button_textview);
        textView.setTypeface(gameActivity.getTypeface());
        textView.setText(locked ? R.string.playershape_name_locked : playerShape.getNameId());
        button = (Button) view.findViewById(R.id.shape_button_button);
        button.setBackgroundDrawable(
                gameActivity.getResources().getDrawable(locked ? R.drawable.playershapes_locked : playerShape.getDrawableId()));
        button.setOnClickListener(this);
        rotation = AnimationUtils.loadAnimation(gameActivity, R.anim.shape_button_rotation);
    }

    private LinearLayout.LayoutParams createLayoutParams(boolean marginToRight) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        if (marginToRight)
            params.setMargins(0, 0, 60, 0);
        return params;
    }

    public void startRotating() {
        button.startAnimation(rotation);
    }

    public void stopRotating() {
        button.clearAnimation();
    }

    @Override
    public void onClick(View v) {
        clickListener.onClick(this);
    }

    public View getView() {
        return view;
    }

    public boolean isLocked() {
        return locked;
    }

    public PlayerShape getPlayerShape() {
        return playerShape;
    }
}
