package com.bernard.mcqinator.view;

import android.app.Activity;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bernard.mcqinator.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Mysaa
 * @date 17/04/17.
 */

public final class TextBackgroundManager {

    private static final Random textRandom = new Random();
    private static String[] deWorte;
    private Integer[] worteYArray;
    private final Activity activity;
    private TextView[] floatingTexts;



    public TextBackgroundManager(Activity activity) {
        this.activity = activity;
        //TODO rearrange preferences
        switch(PreferenceManager.getDefaultSharedPreferences(activity).getInt("preferedTheme",0)){
            case 0:
                activity.setTheme(R.style.DarkTheme);
                break;
            case 1:
                activity.setTheme(R.style.LightTheme);
                break;
        }
        if(deWorte == null)
            deWorte = activity.getResources().getStringArray(R.array.randomWorte);
        final View rout = activity.findViewById(android.R.id.content);
        rout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // gets called after layout has been done but before display
                /*
                TypedArray styledAttributes = TextBackgroundManager.this.activity.obtainStyledAttributes(new int[]{android.R.attr.textSize});
                Pattern p = Pattern.compile("^[0-9]+");
                String textSize = styledAttributes.hasValue(0)?styledAttributes.getString(0):"12";
                Matcher m = p.matcher(textSize);
                Log.d("TXTS",textSize);
                */
                final int txtHeight = 20;
                final int totalHeight = rout.getHeight();
                int currentPos = textRandom.nextInt(txtHeight);
                List<Integer> tempYArray = new ArrayList<>();
                while (currentPos < totalHeight) {
                    tempYArray.add(currentPos);
                    currentPos += txtHeight;
                    currentPos += textRandom.nextInt(8) - 4;
                }
                worteYArray = new Integer[tempYArray.size()];
                tempYArray.toArray(worteYArray);
                rout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                floatingTexts = new TextView[worteYArray.length];
                for (int i = 0; i < worteYArray.length; i++)
                    floatingTexts[i] = launchText(i);
            }
        });
    }

    private TextView launchText(int x){
        final TextView tv = new TextView(activity);
        tv.setText(deWorte[textRandom.nextInt(deWorte.length)]);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
        params.leftMargin = -tv.getWidth();
        params.topMargin = worteYArray[x];
        tv.setLayoutParams(params);
        ((ViewGroup)activity.findViewById(android.R.id.content)).addView(tv,0);
        tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                TranslateAnimation anim = new TranslateAnimation(-tv.getWidth(), tv.getWidth(), 0, 0);
                anim.setDuration(8000 + textRandom.nextInt(1000));
                anim.setStartOffset(textRandom.nextInt(9000));
                anim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        tv.setText(deWorte[textRandom.nextInt(deWorte.length)]);
                        TranslateAnimation anim = new TranslateAnimation(-tv.getWidth(), tv.getWidth(), 0, 0);
                        anim.setDuration(8000 + textRandom.nextInt(1000));
                        anim.setAnimationListener(this);
                        tv.startAnimation(anim);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                tv.startAnimation(anim);
                tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
         });
        return tv;
    }

    public void stopTexts(){
        if(floatingTexts == null)return;
        for (TextView t:floatingTexts) {
            t.clearAnimation();
        }
    }

    public void restartTexts(){
        if(floatingTexts == null)return;
        for (final TextView tv:floatingTexts) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
            params.leftMargin = -tv.getWidth();
            params.topMargin = ((FrameLayout.LayoutParams)tv.getLayoutParams()).topMargin;
            tv.setLayoutParams(params);
            tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    TranslateAnimation anim = new TranslateAnimation(-tv.getWidth(), tv.getWidth(), 0, 0);
                    anim.setDuration(8000 + textRandom.nextInt(1000));
                    anim.setStartOffset(textRandom.nextInt(9000));
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            tv.setText(deWorte[textRandom.nextInt(deWorte.length)]);
                            TranslateAnimation anim = new TranslateAnimation(-tv.getWidth(), tv.getWidth(), 0, 0);
                            anim.setDuration(8000 + textRandom.nextInt(1000));
                            anim.setAnimationListener(this);
                            tv.startAnimation(anim);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                    tv.startAnimation(anim);
                    tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
        }
    }

    public static Random getRandom() {
        return textRandom;
    }
}
