package com.bokecc.livemodule.view.timer;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import android.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author:
 * Date: 2023/1/6
 * Description:
 */
public class DigitalTimerView extends BaseTimerView{
    public DigitalTimerView(Context context) {
        super(context);
    }

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

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



    private ArrayList<ViewUnit> mViewUnitList;
    boolean mIsShowLastSuffix = false; // 是否显示 "秒" 后面的后缀
    ViewUnit mViewUnitStyle; // 显示风格

    ViewUpdater mViewUpdater;
    public DigitalTimerView setViewUpdater(ViewUpdater viewUpdater)  {
        mViewUpdater = viewUpdater;
        return this;
    }
    public interface EndListener{
        void onCall(boolean end);
    }

    private EndListener mEndListener;

    public DigitalTimerView setEndListener(EndListener endListener){
        mEndListener = endListener;
        return this;
    }

    public void onDestroy() {
        stop();
    }

    //重置
    public void reset() {
        stop();
    }

    @Override
    public void onEnd() {
        super.onEnd();
        if (mEndListener != null){
            mEndListener.onCall(true);
        }
    }


    /**
     * 对外接口: 视图更新方式
     */
    interface ViewUpdater {
        /**
         * 初始化 "后缀View", 如设成 "冒号" 或者 "时, 分, 秒" 啥的
         * @param suffixView
         * @param pos
         */
        void initSuffix(View suffixView, int pos);

        /**
         * subTime 指的是 "时, 分, 秒" 这三个小单元
         * @param subTimeView
         * @param subTime
         * @param formattedSubTime
         */
        void updateShow(
                View subTimeView,
                int subTime,
                String formattedSubTime
        );
        /**
         * subTime 天
         * @param subTimeView
         * @param subTime
         * @param formattedSubTime
         */
        void  updateDayShow(
                View subTimeView,
                int subTime,
                String formattedSubTime
        );
        /**
         * subTime 时
         * @param subTimeView
         * @param subTime
         * @param formattedSubTime
         */
        void  updateHourShow(
                View subTimeView,
                int subTime,
                String formattedSubTime
        );
        /**
         * subTime 分
         * @param subTimeView
         * @param subTime
         * @param formattedSubTime
         */
        void  updateMinShow(
                View subTimeView,
                int subTime,
                String formattedSubTime
        );
        /**
         * subTime 秒
         * @param subTimeView
         * @param subTime
         * @param formattedSubTime
         */
        void  updateSecShow(
                View subTimeView,
                int subTime,
                String formattedSubTime
        );


    }

    /**
     * 设置 "时、分、秒" 的显示风格
     * @param rootLayoutRes 布局 xml 文件
     * @param rootLayoutParams  rootLayout 的 LayoutParams, 输入 null 则取默认 WRAP_CONTENT
     * @return
     */
    public DigitalTimerView setHourTimeView(
            int rootLayoutRes,
            ViewGroup.LayoutParams rootLayoutParams
    )  {
        mViewUnitStyle.mHourTimeItem.mRootLayoutRes = rootLayoutRes;
        mViewUnitStyle.mHourTimeItem.mRootLayoutParams = rootLayoutParams;
        return this;
    }

    /**
     * 设置 "时、分、秒" 的显示风格
     * @param rootLayoutRes 布局 xml 文件
     * @param rootLayoutParams  rootLayout 的 LayoutParams, 输入 null 则取默认 WRAP_CONTENT
     * @return
     */
    public DigitalTimerView setMinTimeView(
            int rootLayoutRes,
            ViewGroup.LayoutParams rootLayoutParams
    )  {
        mViewUnitStyle.mMinTimeItem.mRootLayoutRes = rootLayoutRes;
        mViewUnitStyle.mMinTimeItem.mRootLayoutParams = rootLayoutParams;
        return this;
    }
    /**
     * 设置 "时、分、秒" 的显示风格
     * @param rootLayoutRes 布局 xml 文件
     * @param rootLayoutParams  rootLayout 的 LayoutParams, 输入 null 则取默认 WRAP_CONTENT
     * @return
     */
    public DigitalTimerView setSecTimeView(
            int rootLayoutRes,
            ViewGroup.LayoutParams rootLayoutParams
    )  {
        mViewUnitStyle.mSecTimeItem.mRootLayoutRes = rootLayoutRes;
        mViewUnitStyle.mSecTimeItem.mRootLayoutParams = rootLayoutParams;
        return this;
    }
    /**
     * 设置 "天" 的显示风格
     * @param rootLayoutRes 布局 xml 文件
     * @param rootLayoutParams  rootLayout 的 LayoutParams, 输入 null 则取默认 WRAP_CONTENT
     * @return
     */
    public DigitalTimerView  setDayTimeView(
            int rootLayoutRes,
            ViewGroup.LayoutParams rootLayoutParams
    ){
        mViewUnitStyle.mDayTimeItem.mRootLayoutRes = rootLayoutRes;
        mViewUnitStyle.mDayTimeItem.mRootLayoutParams = rootLayoutParams;
        return this;
    }

    /**
     * 设置 "后缀" 的显示风格, 默认为 冒号":"
     * @param rootLayoutRes 布局 xml 文件
     * @param rootLayoutParams  rootLayout 的 LayoutParams, 输入 null 则取默认 WRAP_CONTENT
     * @return
     */
    public DigitalTimerView setSuffixView(
            int rootLayoutRes,
            ViewGroup.LayoutParams rootLayoutParams
    ) {
        mViewUnitStyle.mSuffixItem.mRootLayoutRes = rootLayoutRes;
        mViewUnitStyle.mSuffixItem.mRootLayoutParams = rootLayoutParams;
        return this;
    }


    @Override
    protected void init() {
        super.init();
        this.setOrientation(HORIZONTAL);
        this.setGravity( Gravity.CENTER);
        mViewUnitStyle = new ViewUnit(
                new DayTimeItem(getContext()),
                new HourTimeItem(getContext()),
                new MinTimeItem(getContext()),
                new SecTimeItem(getContext()),
                new SuffixItem(getContext())
        );
    }

    public static final int UNIT_COUNT = 4;



    private void initViewUnitList() {
        removeAllViews();
        mViewUnitList = new ArrayList();

        for (int i=0;i<UNIT_COUNT;i++) {
            ViewUnit viewUnit = new ViewUnit(
                    new DayTimeItem(getContext(),mViewUnitStyle.mDayTimeItem),
                    new HourTimeItem(getContext(), mViewUnitStyle.mHourTimeItem),
                    new MinTimeItem(getContext(), mViewUnitStyle.mMinTimeItem),
                    new SecTimeItem(getContext(), mViewUnitStyle.mSecTimeItem),
            new SuffixItem(getContext(), mViewUnitStyle.mSuffixItem)
            );
            mViewUnitList.add(viewUnit);
            if (i==0){
                this.addView(viewUnit.mDayTimeItem.mRootView);
            }else if(i==1){
                this.addView(viewUnit.mHourTimeItem.mRootView);
            }else if(i==2){
                this.addView(viewUnit.mMinTimeItem.mRootView);
            }else {
                this.addView(viewUnit.mSecTimeItem.mRootView);
            }

            if (i != UNIT_COUNT - 1 || mIsShowLastSuffix) {
                View suffixView = viewUnit.mSuffixItem.mRootView;
                this.addView(suffixView);
                if (mViewUpdater != null) {
                    mViewUpdater.initSuffix(suffixView, i);
                }
            }
        }
    }

    boolean mHasInit = false;

    @Override
    void updateShow(long curTime) {
        if (!mHasInit) {
            mHasInit = true;
            initViewUnitList();
        }
        TimeInfo timeInfo =
                TimeInfo.make(curTime);
        List<Integer> timeList = Arrays.asList(
                (int)timeInfo.getDay(),
                (int)timeInfo.getHour(),
                (int)timeInfo.getMinute(),
                (int)timeInfo.getSecond()
        );
        List<String> formattedTimeList =
                Arrays.asList(
                        timeInfo.getFormatedDay(),
                        timeInfo.getFormatedHour(),
                        timeInfo.getFormatedMinute(),
                        timeInfo.getFormatedSecond()
                );
        int i = 0;
        while (i < mViewUnitList.size() && i < formattedTimeList.size()) {
            if (i==0){//更新天
                if (mViewUpdater != null) {
                    mViewUpdater.updateDayShow(
                            mViewUnitList.get(i).mDayTimeItem.mRootView,
                            timeList.get(i),
                            formattedTimeList.get(i)
                    );
                }
            }else if (i==1){//更新 时
                View subTimeView = mViewUnitList.get(i).mHourTimeItem.mRootView;
                if (mViewUpdater != null) {
                    mViewUpdater.updateHourShow(
                            subTimeView,
                            timeList.get(i),
                            formattedTimeList.get(i)
                    );
                }
            }else if (i==2){//更新 分
                View subTimeView = mViewUnitList.get(i).mMinTimeItem.mRootView;
                if (mViewUpdater != null) {
                    mViewUpdater.updateMinShow(
                            subTimeView,
                            timeList.get(i),
                            formattedTimeList.get(i)
                    );
                }
            }else if (i==3){//更新 秒
                View subTimeView = mViewUnitList.get(i).mSecTimeItem.mRootView;
                if (mViewUpdater != null) {
                    mViewUpdater.updateSecShow(
                            subTimeView,
                            timeList.get(i),
                            formattedTimeList.get(i)
                    );
                }
            }
            i++;
        }
    }

    public static class ViewUnit {

        public Item mDayTimeItem;
        public Item mHourTimeItem;
        public Item mMinTimeItem;
        public Item mSecTimeItem;
//        public Item mSubTimeItem;
        public Item mSuffixItem;

        public ViewUnit(Item mDayTimeItem, Item mHourTimeItem, Item mMinTimeItem, Item mSecTimeItem, Item mSuffixItem) {
            this.mDayTimeItem = mDayTimeItem;
            this.mHourTimeItem = mHourTimeItem;
            this.mMinTimeItem = mMinTimeItem;
            this.mSecTimeItem = mSecTimeItem;
            this.mSuffixItem = mSuffixItem;
        }

        public static class Item{
            private Context context;
            private int mRootLayoutRes;
            private ViewGroup.LayoutParams mRootLayoutParams;

            private View mRootView;

            public Item(Context context) {
                this(context, R.layout.simple_list_item_1,null);
            }

            public Item(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
                this.context = context;
                this.mRootLayoutRes = mRootLayoutRes;
                this.mRootLayoutParams = mRootLayoutParams;
                if (mRootLayoutParams == null) {
                    mRootLayoutParams = new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT
                    );
                }
                mRootView = View.inflate(context, mRootLayoutRes, null);
                mRootView.setLayoutParams(mRootLayoutParams);
            }

            public Item(Context context,Item item) {
                this(context,item.mRootLayoutRes,item.mRootLayoutParams);
            }
        }


    }

//    public static class SubTimeItem extends ViewUnit.Item {
//        public SubTimeItem(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
//            super(context, mRootLayoutRes, mRootLayoutParams);
//        }
//
//        public SubTimeItem(Context context) {
//            super(context);
//        }
//
//        public SubTimeItem(Context context, ViewUnit.Item item) {
//            super(context, item);
//        }
//
//    }
    private static class DayTimeItem extends ViewUnit.Item {
        public DayTimeItem(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
            super(context, mRootLayoutRes, mRootLayoutParams);
        }

        public DayTimeItem(Context context) {
            super(context);
        }

        public DayTimeItem(Context context, ViewUnit.Item item) {
            super(context, item);
        }
    }
    private static class HourTimeItem extends ViewUnit.Item {
        public HourTimeItem(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
            super(context, mRootLayoutRes, mRootLayoutParams);
        }

        public HourTimeItem(Context context) {
            super(context);
        }

        public HourTimeItem(Context context, ViewUnit.Item item) {
            super(context, item);
        }
    }
    private static class MinTimeItem extends ViewUnit.Item {
        public MinTimeItem(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
            super(context, mRootLayoutRes, mRootLayoutParams);
        }

        public MinTimeItem(Context context) {
            super(context);
        }

        public MinTimeItem(Context context, ViewUnit.Item item) {
            super(context, item);
        }
    }
    private static class SecTimeItem extends ViewUnit.Item {
        public SecTimeItem(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
            super(context, mRootLayoutRes, mRootLayoutParams);
        }

        public SecTimeItem(Context context) {
            super(context);
        }

        public SecTimeItem(Context context, ViewUnit.Item item) {
            super(context, item);
        }
    }

    private static class SuffixItem extends ViewUnit.Item {
        public SuffixItem(Context context, int mRootLayoutRes, ViewGroup.LayoutParams mRootLayoutParams) {
            super(context, mRootLayoutRes, mRootLayoutParams);
        }

        public SuffixItem(Context context) {
            super(context);
        }

        public SuffixItem(Context context, ViewUnit.Item item) {
            super(context, item);
        }

    }




}
