package com.mints.goodmoney.ui.widgets;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.mints.goodmoney.R;
import com.mints.goodmoney.ui.widgets.seekbar.BubbleSeekBar;
import com.mints.goodmoney.utils.rxutil.CommonRxTask;
import com.mints.goodmoney.utils.rxutil.RxjavaUtil;
import com.mints.library.utils.nodoubleclick.AntiShake;

import java.util.ArrayList;

/**
 * 描述：走路view
 * 作者：孟崔广
 * 时间：2020/8/31 10:50
 * 邮箱：mengcga@163.com
 */
public class StepView extends FrameLayout
        implements View.OnClickListener {

    // 按钮监听
    private StepViewListener stepViewListener;

    // 浮云金币
    private TextView tvOneWater;
    private TextView tvTwoWater;
    private TextView tvThreeWater;
    private TextView tvFouroneWater;
    private LinearLayout llOneWater;
    private LinearLayout llTwoWater;
    private LinearLayout llThreeWater;
    private LinearLayout llFourWater;

    private int threeCoin;

    // 喝水提示
    private TextView tvStep;
    private TextView tvNext;
    private BubbleSeekBar bsStep;
    private RelativeLayout rlNext;

    // 浮云金币动画
    private ObjectAnimator oneAnimator;
    private ObjectAnimator twoAnimator;
    private ObjectAnimator threeAnimator;
    private ObjectAnimator fourAnimator;

    public StepView(Context context) {
        this(context, null);
    }

    public StepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initListener();
        initBtnAnim();
    }

    @Override
    public void onClick(View v) {
        if (AntiShake.check(v.getId())) return;

        if (stepViewListener != null) {
            switch (v.getId()) {
                case R.id.tv_water_one:
                    stepViewListener.clickWater(1, tvOneWater.getText().toString());
                    cancelWaterAnim(llOneWater);
                    break;
                case R.id.tv_water_two:
                    stepViewListener.clickWater(2, tvTwoWater.getText().toString());
                    cancelWaterAnim(llTwoWater);
                    break;
                case R.id.tv_water_three:
                    stepViewListener.clickWater(3, tvTwoWater.getText().toString());
                    cancelWaterAnim(llThreeWater);
                    break;
                case R.id.tv_water_four:
                    stepViewListener.clickWater(4, tvFouroneWater.getText().toString());
                    cancelWaterAnim(llFourWater);
                    break;
                case R.id.tv_water_next:
                    stepViewListener.clickWater(0, "-1");
                    break;
            }
        }
    }


    /**
     * 初始化view
     *
     * @param context
     */
    private void initView(Context context) {
        View view = View.inflate(context, R.layout.view_step, this);

        // 浮云金币
        tvOneWater = view.findViewById(R.id.tv_water_one);
        tvTwoWater = view.findViewById(R.id.tv_water_two);
        tvThreeWater = view.findViewById(R.id.tv_water_three);
        tvFouroneWater = view.findViewById(R.id.tv_water_four);
        llOneWater = view.findViewById(R.id.ll_water_one);
        llTwoWater = view.findViewById(R.id.ll_water_two);
        llThreeWater = view.findViewById(R.id.ll_water_three);
        llFourWater = view.findViewById(R.id.ll_water_four);

        oneAnimator = startOne(llOneWater, 18.7f, 2400);
        twoAnimator = startOne(llTwoWater, 17.7f, 2200);
        threeAnimator = startOne(llThreeWater, 19f, 2300);
        fourAnimator = startOne(llFourWater, 17f, 2500);

        tvStep = view.findViewById(R.id.tv_step);
        tvNext = view.findViewById(R.id.tv_water_next);
        bsStep = view.findViewById(R.id.bs_step);
        rlNext = view.findViewById(R.id.rl_water_next);

        bsStep.setClickable(false);
        bsStep.setEnabled(false);
        bsStep.setSelected(false);
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        tvOneWater.setOnClickListener(this);
        tvTwoWater.setOnClickListener(this);
        tvThreeWater.setOnClickListener(this);
        tvFouroneWater.setOnClickListener(this);
        tvNext.setOnClickListener(this);
    }

    /**
     * 按钮动画
     */
    private void initBtnAnim() {
        RxjavaUtil.executeRxTask(new CommonRxTask<String>("") {
            @Override
            public void doInIOThread() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void doInUIThread() {
                if (rlNext != null) {
                    YoYo.with(Techniques.Pulse).duration(1200)
                            .interpolate(new AccelerateDecelerateInterpolator())
                            .repeat(-1)
                            .playOn(rlNext);
                }
            }
        });
    }

    /**
     * 设置浮云金币样式
     *
     * @param water
     * @param coin
     */
    public void setWaterTextviewLayout(int water, int coin) {
        switch (water) {
            case 1:
                llOneWater.setVisibility(VISIBLE);
                tvOneWater.setBackground(getResources().getDrawable(R.mipmap.ic_water));
                break;
            case 2:
                llTwoWater.setVisibility(VISIBLE);
                tvTwoWater.setBackground(getResources().getDrawable(R.mipmap.ic_water));
                break;
            case 3:
                llThreeWater.setVisibility(VISIBLE);
                tvThreeWater.setBackground(getResources().getDrawable(R.mipmap.ic_water));
                break;
            case 4:
                llFourWater.setVisibility(VISIBLE);
                tvFouroneWater.setBackground(getResources().getDrawable(R.mipmap.ic_water));
                break;
        }
    }

    /**
     * 设置喝水进度
     *
     * @param progress
     */
    public void setWaterProgress(int progress, int maxProgress) {
        if (bsStep != null) {
            if (progress <= maxProgress) {
                bsStep.setProgress(progress);
            } else {
                bsStep.setProgress(maxProgress);
            }
        }
        if (tvStep != null) {
            tvStep.setText(progress + "");
        }
    }

    /**
     * 设置喝水总进度
     *
     * @param maxProgress
     */
    public void setWaterMaxProgress(int maxProgress) {
        if (bsStep != null) {
            bsStep.setmMax(maxProgress);
        }
    }

    /**
     * 上下浮云动画
     *
     * @param view
     * @return
     */
    private ObjectAnimator startOne(View view, float offset, int time) {
        ObjectAnimator mAnimator = ObjectAnimator.ofFloat(view, "translationY", view.getY() - offset, view.getY() + offset, view.getY() - offset);
        mAnimator.setDuration(time);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.setRepeatMode(ValueAnimator.RESTART);
        mAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mAnimator.start();
        return mAnimator;
    }

    /**
     * 取消上下浮云动画
     *
     * @param llView
     */
    private void cancelWaterAnim(LinearLayout llView) {
//        YoYo.with(Techniques.FadeOutDown)
//                .repeat(0)
//                .playOn(tvView);

//        if (animator != null) {
//            animator.cancel();
//        }

        if (llView != null) {
            llView.setVisibility(INVISIBLE);
        }
    }

    /**
     * 关闭所有动画
     */
    public void destoryAnim() {
        if (oneAnimator != null) {
            oneAnimator.cancel();
            oneAnimator = null;
        }

        if (twoAnimator != null) {
            twoAnimator.cancel();
            twoAnimator = null;
        }

        if (threeAnimator != null) {
            threeAnimator.cancel();
            threeAnimator = null;
        }

        if (fourAnimator != null) {
            fourAnimator.cancel();
            fourAnimator = null;
        }
    }

    public void setStepViewListener(StepViewListener stepViewListener) {
        this.stepViewListener = stepViewListener;
    }

    /**
     * 按钮监听
     */
    public interface StepViewListener {
        /**
         * 点击水滴
         *
         * @param water     -1表示喝水打卡
         * @param waterCoin 水滴金币
         */
        void clickWater(int water, String waterCoin);
    }


    /**
     * 按钮可用
     */
    public void setTvDrinkBtnEnable() {
        if (tvNext != null) {
            tvNext.setEnabled(true);
            tvNext.setBackground(getResources().getDrawable(R.drawable.shape_water_btn));
        }
    }

    /**
     * 按钮显示灰
     */
    public void setTvDrinkBtnNone() {
        if (tvNext != null) {
            tvNext.setEnabled(false);
            tvNext.setBackground(getResources().getDrawable(R.drawable.shape_tv_gold_none));
        }
    }

    /**
     * 按钮内容
     */
    public void setTvDrinkBtnText(String content) {
        if (tvNext != null) {
            tvNext.setText(content);
        }
    }

    /**
     * 首页汽泡清空
     */
    public void setThreeBtnGone() {

        if (llOneWater != null) {
            llOneWater.setVisibility(INVISIBLE);
        }

        if (llTwoWater != null) {
            llTwoWater.setVisibility(INVISIBLE);
        }

        if (llThreeWater != null) {
            llThreeWater.setVisibility(INVISIBLE);
        }

        if (llFourWater != null) {
            llFourWater.setVisibility(INVISIBLE);
        }
    }
}
