package de.frajul.endlessroll.views;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.GameActivity;
import de.frajul.endlessroll.main.screens.Screen;

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

    public interface ScreenSwitchCaller {
        public void switchScreen(Screen.ScreenType screenType);
    }

    public enum MessageType {
        STARS(R.string.bounty_message_resource_format_s, Screen.ScreenType.NONE),
        ENERGY(R.string.bounty_message_resource_format_s, Screen.ScreenType.NONE),
        TOOL(R.string.bounty_message_tool_format_s, Screen.ScreenType.TOOL_SHOP),
        TOOL_SLOT(R.string.bounty_message_tool_slot, Screen.ScreenType.TOOL_SHOP),
        SHAPE_UNLOCKED(R.string.bounty_message_shape_unlocked, Screen.ScreenType.SHAPE_SHOP);

        @StringRes
        private int textId;
        private Screen.ScreenType goalScreenType;

        MessageType(@StringRes int textId, Screen.ScreenType goalScreenType) {
            this.textId = textId;
            this.goalScreenType = goalScreenType;
        }

        public String formatText(Context context, @Nullable String textArgs) {
            String text = context.getString(textId);
            if (textArgs == null)
                return text;
            return String.format(text, textArgs);
        }


        public Screen.ScreenType getGoalScreenType() {
            return goalScreenType;
        }

    }

    private LinearLayout layout;
    private ImageView image;
    private TextView text;

    private MessageType messageType;
    private ScreenSwitchCaller screenSwitchCaller;

    public BountyMessage(GameActivity gameActivity, MessageType messageType, @Nullable String textArgs, ScreenSwitchCaller screenSwitchCaller, @DrawableRes int drawableId) {
        findViews(gameActivity);
        this.image.setImageResource(drawableId);
        this.text.setText(messageType.formatText(gameActivity, textArgs));
        this.text.setTypeface(gameActivity.getTypeface());
        this.messageType = messageType;
        this.screenSwitchCaller = screenSwitchCaller;
    }

    private void findViews(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = (LinearLayout) inflater.inflate(R.layout.unlock_message, null);
        layout.setOnClickListener(this);
        image = (ImageView) layout.findViewById(R.id.unlockmessage_toolimage);
        text = (TextView) layout.findViewById(R.id.unlockmessage_message);
    }

    @Override
    public void onClick(View v) {
        if (messageType.getGoalScreenType() != Screen.ScreenType.NONE)
            screenSwitchCaller.switchScreen(messageType.getGoalScreenType());
    }

    public LinearLayout getLayout() {
        return layout;
    }
}
