package com.bokecc.livemodule.view.timer;


import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;


/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author:
 * Date: 2023/1/6
 * Description:
 */
abstract class BaseTimerView extends LinearLayout implements ITimer, ITimer.OnCountTimeListener {

    public BaseTimerView(Context context) {
        super(context);
        init();
    }

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

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


    private ITimer mTimer = new CountDown();
    private OnCountTimeListener mOnCountTimeListener;

    public void setTimer(ITimer timer) {
        mTimer = timer;
    }


    protected void init() {
        mTimer.setOnCountTimeListener(this);
    }

    @Override
    public void setLifeCycleListener(LifeCycleListener lifeCycleListener) {
        mTimer.setLifeCycleListener(lifeCycleListener);
    }

    @Override
    public void setOnCountTimeListener(OnCountTimeListener onCountTimeListener) {
        mOnCountTimeListener = onCountTimeListener;
    }

    @Override
    public void start(long initTime) {
        mTimer.start(initTime);
    }

    @Override
    public void stop() {
        mTimer.stop();
    }

    @Override
    public void countTime(long curTime) {
        updateShow(curTime);
        if (mOnCountTimeListener != null){
            mOnCountTimeListener.countTime(curTime);
        }
    }

    @Override
    public void onEnd() {
        mTimer.stop();
    }

    abstract void updateShow(long curTime);


}