package com.mints.goodmoney.ui.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.mints.goodmoney.R;
import com.mints.goodmoney.mvp.model.SignViewBean;
import com.mints.library.utils.nodoubleclick.AntiShake;

import java.util.ArrayList;

/**
 * 签到功能
 */
public class SignView extends LinearLayout implements View.OnClickListener {

    // 金币
    private TextView tvFirst;
    private TextView tvTwo;
    private TextView tvThree;
    private TextView tvFour;
    private TextView tvFive;
    private TextView tvSix;
    private TextView tvSeven;

    // 天数
    private TextView tvFirstday;
    private TextView tvTwoday;
    private TextView tvThreeday;
    private TextView tvFourday;
    private TextView tvFiveday;
    private TextView tvSixday;
    private TextView tvSevenday;

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

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

    public SignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //将打气筒根据自定义控件的布局文件，创建的view 对象挂载到当前类上面，然后显示
        View view = View.inflate(context, R.layout.view_sign, this);

        view.findViewById(R.id.ll_sign_firstday).setOnClickListener(this);
        view.findViewById(R.id.ll_sign_twoday).setOnClickListener(this);
        view.findViewById(R.id.ll_sign_threeday).setOnClickListener(this);
        view.findViewById(R.id.ll_sign_fourday).setOnClickListener(this);
        view.findViewById(R.id.ll_sign_fiveday).setOnClickListener(this);
        view.findViewById(R.id.ll_sign_sixday).setOnClickListener(this);
        view.findViewById(R.id.ll_sign_sevenday).setOnClickListener(this);

        tvFirst = view.findViewById(R.id.tv_sign_first);
        tvTwo = view.findViewById(R.id.tv_sign_two);
        tvThree = view.findViewById(R.id.tv_sign_three);
        tvFour = view.findViewById(R.id.tv_sign_four);
        tvFive = view.findViewById(R.id.tv_sign_five);
        tvSix = view.findViewById(R.id.tv_sign_six);
        tvSeven = view.findViewById(R.id.tv_sign_seven);

        tvFirstday = view.findViewById(R.id.tv_sign_firstday);
        tvTwoday = view.findViewById(R.id.tv_sign_twoday);
        tvThreeday = view.findViewById(R.id.tv_sign_threeday);
        tvFourday = view.findViewById(R.id.tv_sign_fourday);
        tvFiveday = view.findViewById(R.id.tv_sign_fiveday);
        tvSixday = view.findViewById(R.id.tv_sign_sixday);
        tvSevenday = view.findViewById(R.id.tv_sign_sevenday);
    }

    /**
     * 初始化签到金币数量
     */
    public void initSignViewGoldLayout(ArrayList<Integer> goldList) {
        if (goldList != null && goldList.size() >= 6) {
            setGoldLayout(tvFirst, goldList.get(0));
            setGoldLayout(tvTwo, goldList.get(1));
            setGoldLayout(tvThree, goldList.get(2));
            setGoldLayout(tvFour, goldList.get(3));
            setGoldLayout(tvFive, goldList.get(4));
            setGoldLayout(tvSix, goldList.get(5));
            setGoldLayout(tvSeven, goldList.get(6));
        } else {
            setGoldLayout(tvFirst, 18);
            setGoldLayout(tvTwo, 28);
            setGoldLayout(tvThree, 38);
            setGoldLayout(tvFour, 48);
            setGoldLayout(tvFive, 58);
            setGoldLayout(tvSix, 68);
            setGoldLayout(tvSeven, 78);
        }
    }

    private void setGoldLayout(TextView tvGold, int gold) {
        tvGold.setText(gold + "");
    }

    /**
     * 设置签到样式
     *
     * @param bean
     */
    public void setSignViewLayout(SignViewBean bean) {
        if (bean == null) return;

        int firstStatus = bean.getFirstStatus();
        int twoStatus = bean.getTwoStatus();
        int threeStatus = bean.getThreeStatus();
        int fourStatus = bean.getFourStatus();
        int fiveStatus = bean.getFiveStatus();
        int sixStatus = bean.getSixStatus();
        int sevenStatus = bean.getSevenStatus();

        setContentLayoutAndBgColor(1, firstStatus, tvFirst, tvFirstday, false);
        setContentLayoutAndBgColor(2, twoStatus, tvTwo, tvTwoday, false);
        setContentLayoutAndBgColor(3, threeStatus, tvThree, tvThreeday, false);
        setContentLayoutAndBgColor(4, fourStatus, tvFour, tvFourday, false);
        setContentLayoutAndBgColor(5, fiveStatus, tvFive, tvFiveday, false);
        setContentLayoutAndBgColor(6, sixStatus, tvSix, tvSixday, false);
        setContentLayoutAndBgColor(7, sevenStatus, tvSeven, tvSevenday, false);
    }

    public void setCurDaysSignViewLayout(int signDays, int status) {
        if (signDays == 1) {
            setDaysLayout(signDays, status, tvFirstday);
            setContentLayoutAndBgColor(signDays, status, tvFirst, tvFirstday, true);
        } else if (signDays == 2) {
            setDaysLayout(signDays, status, tvTwoday);
            setContentLayoutAndBgColor(signDays, status, tvTwo, tvTwoday, true);
        } else if (signDays == 3) {
            setDaysLayout(signDays, status, tvThreeday);
            setContentLayoutAndBgColor(signDays, status, tvThree, tvThreeday, true);
        } else if (signDays == 4) {
            setDaysLayout(signDays, status, tvFourday);
            setContentLayoutAndBgColor(signDays, status, tvFour, tvFourday, true);
        } else if (signDays == 5) {
            setDaysLayout(signDays, status, tvFiveday);
            setContentLayoutAndBgColor(signDays, status, tvFive, tvFiveday, true);
        } else if (signDays == 6) {
            setDaysLayout(signDays, status, tvSixday);
            setContentLayoutAndBgColor(signDays, status, tvSix, tvSixday, true);
        } else if (signDays == 7) {
            setDaysLayout(signDays, status, tvSevenday);
            setContentLayoutAndBgColor(signDays, status, tvSeven, tvSevenday, true);
        }
    }

    private void setDaysLayout(int signDays, int status, TextView tvDay) {
        if (status == 0) {
            tvDay.setTextColor(getResources().getColor(R.color.black));
            tvDay.setText(signDays + "天");
        }
    }

//    public void setSevenAnim(int signDays, int status) {
//        if (status == 0 && signDays == 7 && tvSeven != null) {
//            YoYo.with(Techniques.Swing).duration(1200)
//                    .interpolate(new AccelerateDecelerateInterpolator())
//                    .repeat(15)
//                    .playOn(tvSeven);
//        }
//    }

    /**
     * 设置文字样式
     *
     * @param status    0-未签到 1-已签到 2-已签到且看了视频
     * @param isCurFlag true-当天状态
     */
    private void setContentLayoutAndBgColor(int days, int status, TextView tvContent, TextView tvDay, boolean isCurFlag) {
        Resources resources = getResources();
//        if (UserManager.getInstance().getAdShowFlag()) {
        switch (status) {
            case 0:
                if (days == 7) {
                    tvContent.setText("");
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_close));

                } else {
                    tvDay.setText(days + "天");
                    tvContent.setTextColor(resources.getColor(R.color.my_color_orange2));
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_nofinish));
                }
                tvDay.setTextColor(resources.getColor(R.color.black));
                break;
            case 1:
                if (isCurFlag) {
                    tvContent.setText("");
                    tvContent.setTextColor(resources.getColor(R.color.graya));
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_video));
                    tvDay.setTextColor(resources.getColor(R.color.black));
                    tvDay.setText("可翻倍");
                } else {

                    if (days == 7) {
                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_open));

                    } else {
                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_finish));
                    }

                    tvContent.setText("");
                    tvContent.setTextColor(resources.getColor(R.color.graya));
                    tvDay.setTextColor(resources.getColor(R.color.graya));
                    tvDay.setText("已领");
                }
                break;
            case 2:
                if (days == 7) {
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_open));

                } else {
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_finish));
                }

                tvContent.setText("");
                tvContent.setTextColor(resources.getColor(R.color.graya));
                tvDay.setTextColor(resources.getColor(R.color.graya));
                tvDay.setText("已领");
                break;
            default:
                if (days == 7) {
                    tvContent.setText("");
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_close));
                } else {
                    tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_finish));
                }
                tvDay.setTextColor(resources.getColor(R.color.black));
                break;
        }
//        } else {
//            switch (status) {
//                case 0:
//                    if (days == 7) {
//                        tvContent.setText("");
//                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_close));
//
//                    } else {
//                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_finish));
//                    }
//                    tvDay.setTextColor(resources.getColor(R.color.black));
//                    break;
//                case 1:
//                case 2:
//                    if (days == 7) {
//                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_open));
//
//                    } else {
//                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_none));
//                    }
//
//                    tvContent.setText("");
//                    tvContent.setTextColor(resources.getColor(R.color.graya));
//                    tvDay.setTextColor(resources.getColor(R.color.graya));
//                    tvDay.setText("已领");
//                    break;
//                default:
//                    if (days == 7) {
//                        tvContent.setText("");
//                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_newgift_close));
//                    } else {
//                        tvContent.setBackground(resources.getDrawable(R.mipmap.ic_sign_finish));
//                    }
//                    tvDay.setTextColor(resources.getColor(R.color.black));
//                    break;
//            }
//        }


    }


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

        if (signViewListener != null) {
            switch (v.getId()) {
                case R.id.ll_sign_firstday:
                    signViewListener.clickDays(1);
                    break;
                case R.id.ll_sign_twoday:
                    signViewListener.clickDays(2);
                    break;
                case R.id.ll_sign_threeday:
                    signViewListener.clickDays(3);
                    break;
                case R.id.ll_sign_fourday:
                    signViewListener.clickDays(4);
                    break;
                case R.id.ll_sign_fiveday:
                    signViewListener.clickDays(5);
                    break;
                case R.id.ll_sign_sixday:
                    signViewListener.clickDays(6);
                    break;
                case R.id.ll_sign_sevenday:
                    signViewListener.clickDays(7);
                    break;
            }
        }
    }

    private SignViewListener signViewListener;

    public void setSignViewListener(SignViewListener signViewListener) {
        this.signViewListener = signViewListener;
    }

    public interface SignViewListener {
        /**
         * 点击第几天
         *
         * @param day
         */
        void clickDays(int day);
    }
}
