package de.frajul.endlessroll.views;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

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

/**
 * Created by Julian on 15.07.2016.
 */
public class TaskCompletedMessage implements View.OnClickListener, BountyMessage.ScreenSwitchCaller {

    private GameActivity gameActivity;
    private View layout;

    private LinearLayout messagesLayout;

    public TaskCompletedMessage(GameActivity gameActivity) {
        this.gameActivity = gameActivity;

        LayoutInflater inflater = LayoutInflater.from(gameActivity);
        layout = inflater.inflate(R.layout.task_completed_message, null);
        layout.setOnClickListener(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setVisibility(View.GONE);
        messagesLayout = (LinearLayout) layout.findViewById(R.id.task_completed_unlock_list);
    }

    public void show(List<PlayerShape> shapes) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 8, 0, 8);

        for (PlayerShape shape : shapes)
            addShapeMessage(shape, params);
        layout.setVisibility(View.VISIBLE);
    }

    private void addShapeMessage(PlayerShape shape, LinearLayout.LayoutParams params) {
        BountyMessage message = createBountyMessage(shape);
        messagesLayout.addView(message.getLayout(), params);
    }

    private BountyMessage createBountyMessage(PlayerShape shape) {
        BountyMessage bountyMessage = new BountyMessage(gameActivity, BountyMessage.MessageType.SHAPE_UNLOCKED, null, this,
                shape.getDrawableId());
        return bountyMessage;
    }

    private void hide() {
        layout.setVisibility(View.GONE);
        messagesLayout.removeAllViews();
    }

    public View getLayout() {
        return layout;
    }

    @Override
    public void onClick(View v) {
        hide();
    }

    @Override
    public void switchScreen(Screen.ScreenType screenType) {
        gameActivity.flipToScreen(screenType);
        hide();
    }
}
