package com.bokecc.livemodule.view.timer;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author:
 * Date: 2023/1/6
 * Description:
 */
public interface ITimer {

    void setLifeCycleListener(LifeCycleListener lifeCycleListener);
    void setOnCountTimeListener(OnCountTimeListener onCountTimeListener);
    void start(long initTime);
    void stop();



    /**
     * 生命周期, 用户手动控制
     */
    public interface LifeCycleListener{
        void onStart();
        void onStop();
    }

    /**
     * 计数回调, 计数器自行控制
     */
    public interface OnCountTimeListener{
        void countTime(long curTime);
        void onEnd();
    }

    public class CountDown implements ITimer{
        private OnCountTimeListener mOnCountTimeListener;

        private ScheduledExecutorService mTimerExecutor;

        private Handler sHandler = new Handler(Looper.getMainLooper());

        private LifeCycleListener mLifeCycleListener;

        private long mLeftTime = 0;

        public static final long PERIOD = 1000;

        public CountDown(OnCountTimeListener mOnCountTimeListener) {
            this.mOnCountTimeListener = mOnCountTimeListener;
        }

        public CountDown() {
            mTimerExecutor = Executors.newSingleThreadScheduledExecutor();
        }

        @Override
        public void setLifeCycleListener(LifeCycleListener lifeCycleListener) {
            mLifeCycleListener = lifeCycleListener;
        }
        @Override
        public void setOnCountTimeListener(OnCountTimeListener onCountTimeListener){
            mOnCountTimeListener = onCountTimeListener;
        }

        @Override
        public void start(long initTime){
            if (initTime<0L){
                return;
            }
            showdown();
            mTimerExecutor = Executors.newSingleThreadScheduledExecutor();
            mLeftTime = initTime;
            mTimerExecutor.scheduleAtFixedRate(new Runnable() {
                private Runnable mCountTimeTask = new CountTimeTask();
                @Override
                public void run() {
                    sHandler.post(mCountTimeTask);
                }
            }, 0, PERIOD, TimeUnit.MILLISECONDS);
            if (mLifeCycleListener!=null){
                mLifeCycleListener.onStart();
            }
        }
        @Override
        public void stop(){
            if (!mTimerExecutor.isShutdown()){
                showdown();
                if (mLifeCycleListener!=null){
                    mLifeCycleListener.onStop();
                }
            }
        }

        private void showdown(){
            if (!mTimerExecutor.isShutdown()){
                mTimerExecutor.shutdown();
            }
        }

        private class CountTimeTask implements Runnable {

            @Override
            public void run() {
                mLeftTime -= PERIOD;
                if (mLeftTime>=0){
                    countTime(mLeftTime);
                }else {
                    onEnd();
                }
            }

            private void countTime(long curTime){
                if (mOnCountTimeListener!=null){
                    mOnCountTimeListener.countTime(curTime);
                }
            }

            private void onEnd(){
                if (mOnCountTimeListener!=null){
                    mOnCountTimeListener.onEnd();
                }
            }
        }


    }
}
