package de.frajul.endlessroll.views;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.concurrent.locks.ReentrantLock;

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

/**
 * Created by Julian on 31.10.2017.
 */

public class CreditsDialog extends Dialog implements View.OnTouchListener {

    private GameActivity gameActivity;

    private ImageView ball;
    private Animation rotation;
    private ScrollView scrollView;
    private ReentrantLock scrollViewAnimatorLock;
    private ObjectAnimator scrollViewAnimator;

    public CreditsDialog(GameActivity gameActivity) {
        super(gameActivity);
        this.gameActivity = gameActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.credits_dialog);

        setTypefaceToTextView(R.id.credits_title);
        setTypefaceToTextView(R.id.credits_author);
        setTypefaceToTextView(R.id.credits_testers_title);
        setTypefaceToTextView(R.id.credits_testers);
        setTypefaceToTextView(R.id.credits_sounds_title);
        setTypefaceToTextView(R.id.credits_sounds);
        setTypefaceToTextView(R.id.credits_music_title);
        setTypefaceToTextView(R.id.credits_music);

        ball = (ImageView) findViewById(R.id.credits_ball);
        rotation = AnimationUtils.loadAnimation(gameActivity, R.anim.shape_button_rotation);
        scrollView = ((ScrollView) findViewById(R.id.credits_scrollview));
        scrollView.setOnTouchListener(this);
        scrollViewAnimatorLock = new ReentrantLock();
    }

    private void setTypefaceToTextView(@IdRes int id) {
        TextView textView = (TextView) findViewById(id);
        textView.setTypeface(gameActivity.getTypeface());
    }

    @Override
    public void show() {
        super.show();
        ball.startAnimation(rotation);
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0, 0);

                scrollViewAnimatorLock.lock();
                try {
                    scrollViewAnimator = ObjectAnimator
                            .ofInt(scrollView, "scrollY", scrollView.getBottom());
                    scrollViewAnimator.setDuration(8000);
                    scrollViewAnimator.setInterpolator(new LinearInterpolator());
                    scrollViewAnimator.start();
                } finally {
                    scrollViewAnimatorLock.unlock();
                }
            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        scrollViewAnimatorLock.lock();
        try {
            if (scrollViewAnimator != null)
                scrollViewAnimator.cancel();
        } finally {
            scrollViewAnimatorLock.unlock();
        }
        return false;
    }
}
