package de.frajul.endlessroll.main.tutorial;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

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

/**
 * Created by Julian on 24.02.2017.
 */

public class TutorialView implements View.OnClickListener {

    private View layout;
    private TextView textView;
    private ImageView imageView;
    private GameActivity activity;

    private int currentBreakPoint = 0;
    private List<BreakPoint> breakPoints;

    public TutorialView(GameActivity activity) {
        this.activity = activity;
        LayoutInflater inflater = LayoutInflater.from(activity);
        layout = inflater.inflate(R.layout.tutorial, null);
        layout.setVisibility(View.GONE);
        layout.setOnClickListener(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        textView = (TextView) layout.findViewById(R.id.tutorial_text_view);
        textView.setTypeface(activity.getTypeface());
        imageView = (ImageView) layout.findViewById(R.id.tutorial_image_view);
    }

    public View getLayout() {
        return layout;
    }

    public void show(List<BreakPoint> breakPoints) {
        this.breakPoints = breakPoints;
        currentBreakPoint = 0;
        if (!breakPoints.isEmpty())
            showCurrentBreakPoint();
    }

    public boolean isShowingTutorial() {
        return layout.getVisibility() == View.VISIBLE;
    }

    private void showCurrentBreakPoint() {
        int textId = breakPoints.get(currentBreakPoint).getTextId();
        int imageId = breakPoints.get(currentBreakPoint).getImageId();

        if (textId == -1)
            textView.setVisibility(View.INVISIBLE);
        else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(textId);
        }
        if (imageId == -1)
            imageView.setVisibility(View.GONE);
        else {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(imageId);
        }
        layout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        currentBreakPoint++;
        if (breakPoints.size() <= currentBreakPoint) {
            layout.setVisibility(View.GONE);
            activity.onTutorialViewHidden();
        } else {
            showCurrentBreakPoint();
        }
    }
}
