import { _decorator, Color, Component, Label, Node, ProgressBar, UITransform } from 'cc';
import { att_count_down, widget_item_data } from '../../../data/data';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('count_down')
export class count_down extends Component {
    private m_data:att_count_down = null;
    @property(Node) ProgressBar_Bg:Node = null;
    @property(Node) ProgressBar_Bar:Node = null;
    @property(Node) img_text:Node = null;
    @property(Node) lab_time:Node = null;
    private time_count:number = 0;
    private add_time:number = 0;
    private m_over_call = null;
    private m_widget_data:widget_item_data = null;
    public init(data:att_count_down,on_over_call,widget_data:widget_item_data){

        this.m_data = data;

        this.m_widget_data = widget_data;

        this.m_over_call = on_over_call;

        this.add_time = 0;

        if(this.m_data.font_info!=undefined){
            this.lab_time.getComponent(Label).fontSize = this.m_data.font_info.font_size;
            this.lab_time.getComponent(Label).lineHeight = this.m_data.font_info.font_size;
            this.lab_time.getComponent(Label).color = new Color(this.m_data.font_info.font_color.r,this.m_data.font_info.font_color.g,this.m_data.font_info.font_color.b)
        }

        gameManager.initUiBaseAtt(this.ProgressBar_Bar,this.m_data.ProgressBar_Bar)
        
        gameManager.initUiBaseAtt(this.ProgressBar_Bg,this.m_data.ProgressBar_Bg)
       
        gameManager.initUiBaseAtt(this.img_text,this.m_data.img_text)

        this.ProgressBar_Bg.getComponent(ProgressBar).totalLength = this.m_data.ProgressBar_Bg.width;
    
        if(this.m_data.time_count>0){
            if(this.m_data.is_show_time){
                this.lab_time.getComponent(Label).string = `${this.m_data.time_count}秒`
                this.lab_time.active = true;
            }else{
                this.lab_time.active = false;
            }
        }else{
            this.lab_time.active = false;
        }


    }

    public getData(){
        return this.m_data
    }

    public addTime(widget_id:number,time:number){
        if(this.m_widget_data.att.id==widget_id){
            this.add_time+=time;
        }
    }

    public startCountDown(){
        this.schedule(()=>{
            this.time_count+=1;
            let all_time = this.m_data.time_count+this.add_time
            if(this.time_count>all_time){
                this.unscheduleAllCallbacks()
                if(this.m_over_call!=null){
                    this.m_over_call(true)
                }
                return
            }
            this.ProgressBar_Bg.getComponent(ProgressBar).progress = 1-this.time_count/all_time
            if(this.m_data.is_show_time){
                this.lab_time.getComponent(Label).string = `${all_time-this.time_count}秒`
            }
           
        },1)
    }



}


