import Common5 from "../../Platform/th/Common5";


const { ccclass, property } = cc._decorator;

@ccclass
export default class DaoJiShiRoll extends cc.Component {
    @property({
        type: cc.Node,
        displayName: "十位分",
    })
    minute_ten: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: "个位分",
    })
    minute_one: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: "十位秒",
    })
    second_ten: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: "个位秒",
    })
    second_one: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: "十位毫秒",
    })
    millisecond_ten: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: "个位毫秒",
    })
    millisecond_one: cc.Node = null;

    @property({
        displayName: "艺术字高度",
    })
    wordHeight: number = 70;

    @property({
        displayName: "总时间s",
    })
    allTimeNum: number = 60;

    @property({
        displayName: "是否为滚动计时器",
    })
    isRollTimer: boolean = false;

    @property({
        type: cc.AudioSource,
        displayName: "倒计时音效",
    })
    timePasses: cc.AudioSource = null;

    isStartTimer: boolean = false    //是否开始倒计时
    finishCallFunc = null   //结束回调
    callTarget = null   //回调对象
    curTimeNum: number = 0; //当前时间

    start() {
        if (!this.allTimeNum) {
            this.allTimeNum = 60;
        }
        this.setRollTime(this.allTimeNum);
    }

    update(dt) {
        if (this.isStartTimer && this.curTimeNum > 0) {
            this.curTimeNum -= dt;
            if (this.curTimeNum <= 0) {
                this.curTimeNum = 0;
                this.isStartTimer = false;
                this.millisecond_ten.y = 0.5 * this.wordHeight;
                this.millisecond_one.y = 0.5 * this.wordHeight;
                this.timePasses.stop();
                this.finishCallFunc && (this.callTarget ? this.finishCallFunc.apply(this.callTarget) : this.finishCallFunc());
                return;
            }
            if (this.isRollTimer) {
                this.updateRollTime(dt);
            } else {
                this.updateShowTime(this.curTimeNum);
            }

        }
    }

    //设置开始/结束倒计时
    setIsStartTimer(bStart: boolean, finishCallFunc?: { target: any, callFunc: any }) {
        this.isStartTimer = bStart;
        if (finishCallFunc) {
            this.finishCallFunc = finishCallFunc.callFunc;
            this.callTarget = finishCallFunc.target;
        }
        if (bStart) {
            this.timePasses.play();
        } else {
            this.timePasses.stop();
        }
    }

    //设置滚动时间
    setRollTime(second: number) {
        if (second >= 3600) {
            second = 3599
        }
        this.curTimeNum = second;

        let num = Math.trunc(second / 600);
        this.minute_ten.y = (num + 0.5) * this.wordHeight;

        second = second % 600;
        num = Math.trunc(second / 60);
        this.minute_one.y = (num + 0.5) * this.wordHeight;

        second = second % 60;
        num = Math.trunc(second / 10);
        this.second_ten.y = (num + 0.5) * this.wordHeight;

        second = second % 10;
        num = Math.trunc(second);
        this.second_one.y = (num + 0.5) * this.wordHeight;

        this.millisecond_ten.y = 0.5 * this.wordHeight;
        this.millisecond_one.y = 0.5 * this.wordHeight;
    }

    //设置滚动时间
    private updateShowTime(second: number) {
        if (second >= 3600) {
            second = 3599
        }
        this.curTimeNum = second;

        let num = Math.trunc(second / 600);
        this.minute_ten.y = (num + 0.5) * this.wordHeight;

        second = second % 600;
        num = Math.trunc(second / 60);
        this.minute_one.y = (num + 0.5) * this.wordHeight;

        second = second % 60;
        num = Math.trunc(second / 10);
        this.second_ten.y = (num + 0.5) * this.wordHeight;

        second = second % 10;
        num = Math.trunc(second);
        this.second_one.y = (num + 0.5) * this.wordHeight;

        second = second * 100 % 100;
        num = Math.trunc(second / 10);
        this.millisecond_ten.y = (num + 0.5) * this.wordHeight;

        second = second % 10;
        num = Math.trunc(second);
        this.millisecond_one.y = (num + 0.5) * this.wordHeight;
    }

    //更新时间帧滚动
    private updateRollTime(dt: number) {

        this.millisecond_one.y -= dt * this.wordHeight * 100;
        if (this.millisecond_one.y < this.wordHeight / 2) {
            let dValue = (this.wordHeight / 2 - this.millisecond_one.y) % (10 * this.wordHeight);
            this.millisecond_one.y = 10.5 * this.wordHeight - dValue;
        }

        this.millisecond_ten.y -= dt * this.wordHeight * 10;
        if (this.millisecond_ten.y >= this.wordHeight / 2) {
            return;
        }
        let dValue = (this.wordHeight / 2 - this.millisecond_ten.y);
        this.millisecond_ten.y = 10.5 * this.wordHeight - dValue;

        this.updateRollTime_second();
    }

    //更新秒级时间滚动
    private updateRollTime_second() {
        if (this.second_one.y > this.wordHeight / 2) {
            cc.tween(this.second_one).to(0.3, { y: this.second_one.y - this.wordHeight }).start();
            return;
        }
        this.second_one.y = 10.5 * this.wordHeight;
        cc.tween(this.second_one).to(0.3, { y: this.second_one.y - this.wordHeight }).start();

        if (this.second_ten.y > this.wordHeight / 2) {
            cc.tween(this.second_ten).to(0.3, { y: this.second_ten.y - this.wordHeight }).start();
            return;
        }
        this.second_ten.y = 6.5 * this.wordHeight;
        cc.tween(this.second_ten).to(0.3, { y: this.second_ten.y - this.wordHeight }).start();

        if (this.minute_one.y > this.wordHeight / 2) {
            cc.tween(this.minute_one).to(0.3, { y: this.minute_one.y - this.wordHeight }).start();
            return;
        }
        this.minute_one.y = 10.5 * this.wordHeight;
        cc.tween(this.minute_one).to(0.3, { y: this.minute_one.y - this.wordHeight }).start();

        if (this.minute_ten.y > this.wordHeight / 2) {
            cc.tween(this.minute_ten).to(0.3, { y: this.minute_ten.y - this.wordHeight }).start();
            return;
        }
        this.minute_ten.y = 6.5 * this.wordHeight;
        cc.tween(this.minute_ten).to(0.3, { y: this.minute_ten.y - this.wordHeight }).start();
    }
}