package com.mints.goodmoney.ui.widgets;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
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.countdowntimer.CountDownTimerSupport;
import com.mints.goodmoney.ui.widgets.countdowntimer.OnCountDownTimerListener;
import com.mints.goodmoney.utils.TimeRender;
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
 * 作者：孟崔广
 * 时间：2019/12/31 18:30
 * 邮箱：mengcga@163.com
 */
public class WaterView extends FrameLayout
        implements View.OnClickListener {

    // 按钮监听
    private WaterViewListener waterViewListener;

    // 浮云金币
    private TextView tvOneWater;
    private TextView tvTwoWater;
    private TextView tvThreeWater;
    private TextView tvFouroneWater;
    private LinearLayout llFouroneWater;

    private int threeCoin;

    // 喝水提示
    private TextView tvDrinkhint;
    private TextView tvDrink;
    private TextView tvNext;
    private TextView tvWaterDrinktime;
    private RelativeLayout rlNext;

    // 喝水进度
    private CircleProgress cpWater;

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

    CountDownTimerSupport waterTimer = null;
    long countDownInterval = 1000;//间隔时长


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

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

    public WaterView(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 (waterViewListener != null) {
            switch (v.getId()) {
                case R.id.tv_water_one:
                    waterViewListener.clickWater(1, tvOneWater.getText().toString());
                    cancelWaterAnim(tvOneWater);
                    break;
                case R.id.tv_water_two:
                    waterViewListener.clickWater(2, tvTwoWater.getText().toString());
                    cancelWaterAnim(tvTwoWater);
                    break;
                case R.id.tv_water_three:
//                    waterViewListener.clickWater(3, tvThreeWater.getText().toString());
                    waterViewListener.clickWater(3, threeCoin + "");
                    cancelWaterAnim(tvThreeWater);
                    break;
                case R.id.tv_water_four:
                    waterViewListener.clickWater(4, tvFouroneWater.getText().toString());
//                    if (llFouroneWater != null) {
//                        llFouroneWater.setVisibility(INVISIBLE);
//                    }
                    break;
                case R.id.tv_water_next:
                    waterViewListener.clickWater(0, "-1");
                    break;
            }
        }
    }


    /**
     * 初始化view
     *
     * @param context
     */
    private void initView(Context context) {
        View view = View.inflate(context, R.layout.view_water, 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);
        llFouroneWater = view.findViewById(R.id.ll_water_four);

//        oneAnimator = startOne(tvOneWater, 18.7f, 2400);
//        twoAnimator = startOne(tvTwoWater, 17.7f, 2200);
//        threeAnimator = startOne(tvThreeWater, 19f, 2300);
//        fourAnimator = startOne(llFouroneWater, 17f, 2500);

        // 喝水提示
        tvDrinkhint = view.findViewById(R.id.tv_water_drinkhint);
        tvDrink = view.findViewById(R.id.tv_water_drink);
        tvNext = view.findViewById(R.id.tv_water_next);
        rlNext = view.findViewById(R.id.rl_water_next);
        tvWaterDrinktime = view.findViewById(R.id.tv_water_drinktime);

        // 喝水进度
        cpWater = view.findViewById(R.id.cp_water);
    }

    /**
     * 初始化监听
     */
    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);
                }
            }
        });
    }

    /**
     * 获取下一个要显示的浮云view
     *
     * @return
     */
    public int getLastShowWaterTextview() {
//        if (tvOneWater != null && tvOneWater.getVisibility() == View.INVISIBLE) {
//            return 1;
//        } else if (tvTwoWater != null && tvTwoWater.getVisibility() == View.INVISIBLE) {
//            return 2;
//        } else if (tvThreeWater != null && tvThreeWater.getVisibility() == View.INVISIBLE) {
//            return 3;
//        } else if (tvFouroneWater != null && tvFouroneWater.getVisibility() == View.INVISIBLE) {
//            return 4;
//        } else {
//            return -1;
//        }

        if (tvOneWater != null && tvOneWater.getVisibility() == View.INVISIBLE) {
            return 1;
        } else if (tvTwoWater != null && tvTwoWater.getVisibility() == View.INVISIBLE) {
            return 2;
        } else if (tvThreeWater != null && tvThreeWater.getVisibility() == View.INVISIBLE) {
            return 3;
        } else {
            return -1;
        }
    }

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

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

    /**
     * 设置喝水总进度
     *
     * @param maxProgress
     */
    public void setWaterMaxProgress(int maxProgress) {
        if (cpWater != null && tvDrink != null) {
            cpWater.setMaxValue(maxProgress);
            tvDrink.setText("0/" + maxProgress + "ml");
        }
    }

    /**
     * 上下浮云动画
     *
     * @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;
    }

    public ArrayList<Integer> getShowWaterList() {
        ArrayList<Integer> waterList = new ArrayList<>();
        if (tvOneWater != null && tvOneWater.getVisibility() == View.VISIBLE) {
            waterList.add(Integer.valueOf(tvOneWater.getText().toString()));
        }
        if (tvTwoWater != null && tvTwoWater.getVisibility() == View.VISIBLE) {
            waterList.add(Integer.valueOf(tvTwoWater.getText().toString()));
        }
        if (tvThreeWater != null && tvThreeWater.getVisibility() == View.VISIBLE) {
//            waterList.add(Integer.valueOf(tvThreeWater.getText().toString()));
            waterList.add(threeCoin);
        }
//        if (tvFouroneWater != null && tvFouroneWater.getVisibility() == View.VISIBLE) {
//            waterList.add(Integer.valueOf(tvFouroneWater.getText().toString()));
//        }

        return waterList;
    }

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

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

        if (tvView != null) {
            tvView.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 setWaterViewListener(WaterViewListener waterViewListener) {
        this.waterViewListener = waterViewListener;
    }

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


    /**
     * 显示第4个喝水汽泡
     */
    public void setFourWaterViewVisible(int coin) {
        if (tvFouroneWater != null && llFouroneWater != null) {
            llFouroneWater.setVisibility(VISIBLE);
            tvFouroneWater.setText(coin + "");
            tvFouroneWater.setBackground(getResources().getDrawable(R.mipmap.ic_water));
        }
    }

    /**
     * 隐藏第4个喝水汽泡
     */
    public void setFourWaterViewGone() {
        if (llFouroneWater != null) {
            llFouroneWater.setVisibility(INVISIBLE);
        }
    }

    /**
     * 获取第4个喝水汽泡状态 true-时间未显示，可点击
     */
    public boolean getBtnWaterViewStatus() {
        if (tvWaterDrinktime != null) {
            if (tvWaterDrinktime.getVisibility() == VISIBLE) {
                return false;
            } else {
                return true;
            }
        }

        return true;
    }

    /**
     * 设置喝水倒计时
     */
    public void setWaterViewTime(Activity activity, int waterTime) {
        if (waterTime > 0) {
            if (waterTimer != null) {//防止计时器重复
                waterTimer.stop();
                waterTimer = null;
            }
            waterTimer = new CountDownTimerSupport((int) (waterTime * countDownInterval), countDownInterval);
            waterTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (activity != null && !activity.isFinishing()) {
                        if (tvWaterDrinktime != null) {
                            tvWaterDrinktime.setVisibility(VISIBLE);
                            tvWaterDrinktime.setText(TimeRender.ms2HMS((int) (millisUntilFinished)) + "后喝水领金币");
                        }

                        if (tvNext != null) {
                            tvNext.setText("喝水打卡");
                        }
                    }
                }

                @Override
                public void onFinish() {
                    if (activity != null && !activity.isFinishing()) {
                        if (tvWaterDrinktime != null) {
                            tvWaterDrinktime.setVisibility(INVISIBLE);
                        }

                        if (tvNext != null) {
                            tvNext.setText("喝水领金币");
                        }
                    }
                }
            });
            waterTimer.start();
        }
    }

    /**
     * 设置喝水倒计时
     */
    public void setWaterViewTimeGone() {
        closeWaterViewTime();
        if (tvWaterDrinktime != null) {
            tvWaterDrinktime.setVisibility(INVISIBLE);
        }
    }

    /**
     * 按钮可用
     */
    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 closeWaterViewTime() {
        if (waterTimer != null) {//防止计时器重复
            waterTimer.stop();
            waterTimer = null;
        }
    }

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

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

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

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