package com.unibuy.smartdevice.tools;

import com.unibuy.smartdevice.exception.ErrorCode;

import java.util.concurrent.TimeUnit;

public abstract class HandlerMainCountdown extends HandlerMainAdpter {
    private long countdown;
    private long srcCountdown;

    public HandlerMainCountdown(HandlerMain srcHandlerMain) {
        super(srcHandlerMain);
    }

    public void setCountdown(long countdown) {
        this.countdown = countdown;
        this.srcCountdown = countdown;
    }

    public long getSrcCountdown() {
        return srcCountdown;
    }

    public long getCountdown() {
        return countdown;
    }

    public void start(long countdown) {
        this.countdown = countdown;
        this.srcCountdown = countdown;
        start(0, 1, TimeUnit.SECONDS);
    }

    @Override
    protected void execute(HandlerMain handlerMain) {
        execute(countdown, handlerMain);

        if (countdown < 1) {
            close(handlerMain);
            setRun(false);
        } else {
            countdown--;
        }
    }

    @Override
    protected void close(HandlerMain handlerMain) {

    }

    protected abstract void execute(long countdown, HandlerMain handlerMain);
}
