package kr.co.hanwha.hifive.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 2. 1..
 */

public class HFEvaluationView extends RelativeLayout {
    private RelativeLayout mLayoutBackground;
    private ImageButton mImageButtonGreen;
    private ImageButton mImageButtonYellow;
    private ImageButton mImageButtonRed;
    private ImageButton mImageButtonGray;

    private final int duration = 400;

    private OnEvaluationListener onEvaluationListener;

    public void setOnEvaluationListener(OnEvaluationListener onEvaluationListener) {
        this.onEvaluationListener = onEvaluationListener;
    }

    public HFEvaluationView(Context context) {
        super(context);
        initialize(context, null);
    }

    public HFEvaluationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public HFEvaluationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_evaluation, this, true);

        if (isInEditMode()) return;

        mLayoutBackground = (RelativeLayout) findViewById(R.id.background_relativeLayout);
        mImageButtonGreen = (ImageButton) findViewById(R.id.green_imageButton);
        mImageButtonYellow = (ImageButton) findViewById(R.id.yellow_imageButton);
        mImageButtonRed = (ImageButton) findViewById(R.id.red_imageButton);
        mImageButtonGray = (ImageButton) findViewById(R.id.gray_imageButton);

        mLayoutBackground.setOnClickListener(onClickListener);
        mImageButtonGreen.setOnClickListener(onClickListener);
        mImageButtonYellow.setOnClickListener(onClickListener);
        mImageButtonRed.setOnClickListener(onClickListener);
        mImageButtonGray.setOnClickListener(onClickListener);
    }

    public void showFloating(int x, int y) {
        int screenWidth = HFCommonUtil.getScreenWidth(getContext());
        int screenHeight = HFCommonUtil.getScreenHeight(getContext());
        int radius = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.floating_animation_radius);
        int radians = 45;
        int menuWidth = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.floating_menu_width);

        if (x < radius + menuWidth) {
            radians += (int) (((double) (menuWidth + radius - x) / (double) radius) * 45.0f);
            if (y < radius + menuWidth) radians += 90;
        } else if (x + radius + menuWidth > screenWidth) {
            radians -= (int) (((double) (x + radius + menuWidth - screenWidth) / (double) radius) * 45.0f);
            if (y < radius + menuWidth) radians += 270;
        } else {
            if (y < radius + menuWidth) radians += 180;
        }

        int touchX = x - screenWidth / 2;
        int touchY = y - screenHeight / 2;
//        mImageButtonTouch.setTranslationX(touchX);
//        mImageButtonTouch.setTranslationY(touchY);

        int requestGreenX = touchX - (int) (Math.cos(Math.toRadians(radians + 0)) * radius);
        int requestGreenY = touchY - (int) (Math.sin(Math.toRadians(radians + 0)) * radius);

        int requestYellowX = touchX - (int) (Math.cos(Math.toRadians(radians + 30)) * radius);
        int requestYellowY = touchY - (int) (Math.sin(Math.toRadians(radians + 30)) * radius);

        int requestRedX = touchX - (int) (Math.cos(Math.toRadians(radians + 60)) * radius);
        int requestRedY = touchY - (int) (Math.sin(Math.toRadians(radians + 60)) * radius);

        int requestGrayX = touchX - (int) (Math.cos(Math.toRadians(radians + 90)) * radius);
        int requestGrayY = touchY - (int) (Math.sin(Math.toRadians(radians + 90)) * radius);

        ObjectAnimator animXGreen = ObjectAnimator.ofFloat(mImageButtonGreen, "translationX", touchX, requestGreenX);
        animXGreen.setDuration(duration);
        animXGreen.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
        ObjectAnimator animYGreen = ObjectAnimator.ofFloat(mImageButtonGreen, "translationY", touchY, requestGreenY);
        animYGreen.setDuration(duration);
        animYGreen.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
        AnimatorSet animSetGreen = new AnimatorSet();
        animSetGreen.playTogether(animXGreen, animYGreen);
        animSetGreen.start();

        getAnimatorSet(mImageButtonYellow, touchX, requestYellowX, touchY, requestYellowY).start();

        getAnimatorSet(mImageButtonRed, touchX, requestRedX, touchY, requestRedY).start();

        getAnimatorSet(mImageButtonGray, touchX, requestGrayX, touchY, requestGrayY).start();

        /*
        Animation animGreen = new TranslateAnimation(touchX, requestGreenX, touchY, requestGreenY);
        animGreen.setDuration(duration);
        animGreen.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
        animGreen.setFillAfter(true);
        mImageButtonGreen.startAnimation(animGreen);

        Animation animYellow = new TranslateAnimation(touchX, requestYellowX, touchY, requestYellowY);
        animYellow.setDuration(duration);
        animYellow.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
        animYellow.setFillAfter(true);
        mImageButtonYellow.startAnimation(animYellow);

        Animation animRed = new TranslateAnimation(touchX, requestRedX, touchY, requestRedY);
        animRed.setDuration(duration);
        animRed.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
        animRed.setFillAfter(true);
        mImageButtonRed.startAnimation(animRed);

        Animation animGray = new TranslateAnimation(touchX, requestGrayX, touchY, requestGrayY);
        animGray.setDuration(duration);
        animGray.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));
        animGray.setFillAfter(true);
        mImageButtonGray.startAnimation(animGray);
        */

        mLayoutBackground.setVisibility(View.VISIBLE);
        Animation animFadeIn = AnimationUtils.loadAnimation(getContext(), R.anim.fade_in);
        mLayoutBackground.startAnimation(animFadeIn);
    }

    private AnimatorSet getAnimatorSet(Object target, int touchX, int requestX, int touchY, int requestY) {
        ObjectAnimator animX = ObjectAnimator.ofFloat((View) target, "translationX", touchX, requestX);
        animX.setDuration(duration);
        animX.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));

        ObjectAnimator animY = ObjectAnimator.ofFloat((View) target, "translationY", touchY, requestY);
        animY.setDuration(duration);
        animY.setInterpolator(AnimationUtils.loadInterpolator(getContext(), android.R.anim.overshoot_interpolator));

        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(animX, animY);
        return animSet;

    }

    private void hideFloating() {
        Animation animFadeOut = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out);
        animFadeOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (onEvaluationListener != null) onEvaluationListener.onDismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mLayoutBackground.startAnimation(animFadeOut);
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.green_imageButton:
                    if (onEvaluationListener != null) onEvaluationListener.onGreenClicked();
                    hideFloating();
                    break;
                case R.id.yellow_imageButton:
                    if (onEvaluationListener != null) onEvaluationListener.onYellowClicked();
                    hideFloating();
                    break;
                case R.id.red_imageButton:
                    if (onEvaluationListener != null) onEvaluationListener.onRedClicked();
                    hideFloating();
                    break;
                case R.id.gray_imageButton:
                    if (onEvaluationListener != null) onEvaluationListener.onGrayClicked();
                    hideFloating();
                    break;
                case R.id.background_relativeLayout:
                    hideFloating();
                    break;
            }
        }
    };

    /**************************************************
     * Listener
     **************************************************/
    public interface OnEvaluationListener {
        void onDismiss();

        void onGreenClicked();

        void onYellowClicked();

        void onRedClicked();

        void onGrayClicked();
    }
}
