package hr.fer.labust.diverapp.fragment;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import hr.fer.labust.diverapp.MainActivity;
import hr.fer.labust.diverapp.R;

public class IdleFragment extends Fragment {
    private View mView = null;
    private FrameLayout mLayout;
    private MainActivity mActivity;
    private final ObjectAnimator mAnimator = new ObjectAnimator();

    private static IdleFragment instance;
    private static final Handler mHandler = new Handler();
    private static final Runnable mSetToIdleRunnable = new Runnable() {
        @Override
        public void run() {
            mState = STATE_IDLE;
            if (instance != null) {
                instance.update();
            }
        }
    };

    public static final int STATE_IDLE = 0;
    public static final int STATE_MESSAGE_RECEIVED = 1;
    public static final int STATE_ERROR = 2;
    public static final int STATE_ACKNOWLEDGED = 3;
    public static final int STATE_LOST_COMMUNICATION = 4;
    private static int mState = STATE_IDLE;
    public static boolean messageReceived = false;

    public static void setState(int state) {
        mState = state;

        if (mState == STATE_ACKNOWLEDGED) {
            mHandler.postDelayed(mSetToIdleRunnable, 3500);
        }
    }

    public static int getState() {
        return mState;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity)getActivity();
        instance = this;

        mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        mAnimator.setRepeatMode(ObjectAnimator.RESTART);
        mAnimator.setPropertyName("backgroundColor");
        mAnimator.setIntValues(Color.WHITE, Color.BLACK);
        mAnimator.setEvaluator(new ArgbEvaluator());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_idle, container, false);
            mLayout = (FrameLayout)mView.findViewById(R.id.idle_blinking_layout);
            mAnimator.setTarget(mLayout);
        }
        else {
            ViewGroup parent = (ViewGroup)mView.getParent();

            if (parent != null) {
                parent.removeView(mView);
            }
        }

        update();
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        mActivity.setPreviousTabIndex();
    }

    public void update() {
        switch(mState) {
            case STATE_IDLE:
                mAnimator.setIntValues(Color.WHITE, Color.BLACK);
                mAnimator.setRepeatMode(ObjectAnimator.RESTART);
                mAnimator.setDuration(2000);
                break;
            case STATE_MESSAGE_RECEIVED:
                mAnimator.setIntValues(Color.YELLOW, Color.parseColor("#7f7f00"));
                mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                mAnimator.setDuration(200);
                break;
            case STATE_ERROR:
                mAnimator.setIntValues(Color.RED, Color.parseColor("#7f0000"));
                mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                mAnimator.setDuration(200);
                break;
            case STATE_LOST_COMMUNICATION:
                mAnimator.setIntValues(Color.BLUE, Color.parseColor("#00007f"));
                mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                mAnimator.setDuration(200);
                break;
            case STATE_ACKNOWLEDGED:
                mAnimator.setIntValues(Color.GREEN, Color.parseColor("#007f00"));
                mAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                mAnimator.setDuration(500);
                break;
        }

        mAnimator.start();
    }
}
