import { _decorator, Color, Component, EditBox, Label, Node, Sprite, Toggle } from 'cc';
import { att_count_down, event_item, font_color_info, font_info } from '../../../data/data';
import { tools } from '../../tools';
import { question_btn_info } from './question_btn_info';
import { config } from '../../config';
import { Attributes } from '../Attributes';
const { ccclass, property } = _decorator;

@ccclass('attributes_count_down')
export class attributes_count_down extends Component {
    @property(Node) ProgressBar_Bg:Node = null;
    @property(Node) ProgressBar_Bar:Node = null;
    @property(Node) img_text:Node = null;
    @property(Node) toggle_enbale_time:Node = null;
    @property(Node) edit_down_count_time:Node = null;

      //event
    @property(Node) btn_finish_event:Node = null;
    @property(Node) btn_fail_event:Node = null;
    @property(Node) btn_start_event:Node = null;

    @property(Node) lab_finish_event:Node = null;
    @property(Node) lab_fail_event:Node = null;
    @property(Node) lab_start_event:Node = null;

    @property(Node) clear_finish_event:Node = null;
    @property(Node) clear_fail_event:Node = null;
    @property(Node) clear_start_event:Node = null;

    @property(Node) font_size:Node = null;
    @property(Node) font_color:Node = null;


    private call_back = null;
    private m_data:att_count_down = null;
    public initView(call){
        this.call_back = call;
        this.edit_down_count_time.on('editing-did-ended',()=>{
            this.change()
            this.update_att(this.m_data)
        })
        this.toggle_enbale_time.on('toggle',()=>{
            this.change()
            this.update_att(this.m_data)
        })

        this.btn_finish_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.finish_event_id = data.event_id;
                this.change()
                this.update_att(this.m_data)
            })
        })

        this.btn_fail_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.fail_event_id = data.event_id;
                this.change()
                this.update_att(this.m_data)
            })
        })

        this.btn_start_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                if(data.type!=config.event_type.collect_event){
                    return tools.showToast("必须设置收集事件")
                }
                this.m_data.start_event_id = data.event_id;
                this.change()
                this.update_att(this.m_data)
            })
        })

        this.clear_finish_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.finish_event_id = -1;
            this.change()
            this.update_att(this.m_data)
        })

        this.clear_fail_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.fail_event_id = -1;
            this.change()
            this.update_att(this.m_data)
        })

        this.clear_start_event.on(Node.EventType.TOUCH_END,()=>{
            this.m_data.start_event_id = -1;
            this.change()
            this.update_att(this.m_data)
        })

        this.font_color.on(Node.EventType.TOUCH_END,()=>{
            tools.show_set_color(this.font_color.getComponent(Sprite).color,(color:Color)=>{
                this.m_data.font_info.font_color = new font_color_info().infoColor(color) ;
                this.update_att(this.m_data)
                this.change()
            })
        })

        this.font_size.on('editing-did-ended',()=>{
            this.change()
            this.update_att(this.m_data)
        })

    }
    public update_att(data:att_count_down){
        this.m_data = data;        
        this.ProgressBar_Bg.getComponent(question_btn_info).initView(this.m_data.ProgressBar_Bg,1,null,config.attributes_list_type.count_down)
        this.ProgressBar_Bar.getComponent(question_btn_info).initView(this.m_data.ProgressBar_Bar,2,null,config.attributes_list_type.count_down)
        this.img_text.getComponent(question_btn_info).initView(this.m_data.img_text,3,null,config.attributes_list_type.count_down)
        this.toggle_enbale_time.getComponent(Toggle).isChecked = this.m_data.is_show_time;
        this.edit_down_count_time.getComponent(EditBox).string = this.m_data.time_count.toString()
        this.lab_finish_event.getComponent(Label).string = this.m_data.finish_event_id!=-1?`绑定的id${this.m_data.finish_event_id}`:"无"
        this.lab_fail_event.getComponent(Label).string = this.m_data.fail_event_id!=-1?`绑定的id${this.m_data.fail_event_id}`:"无"
        this.lab_start_event.getComponent(Label).string = this.m_data.start_event_id!=-1?`绑定的id${this.m_data.start_event_id}`:"无"
        if(this.m_data.font_info!=undefined&&this.m_data.font_info!=null){
            let color = this.m_data.font_info.font_color;
            this.font_color.getComponent(Sprite).color = new Color(color.r,color.g,color.b);
            let font_size_string = this.m_data.font_info.font_size.toString()
            if(font_size_string.length > 0) {
                this.font_size.getComponent(EditBox).string = font_size_string
            }
        }else{
            this.m_data.font_info = new font_info;
        }

    }

    change(){
        this.m_data.is_show_time = this.toggle_enbale_time.getComponent(Toggle).isChecked ;
        let down_count_time = this.edit_down_count_time.getComponent(EditBox).string
        if(down_count_time.length>0) {
            this.m_data.time_count = parseInt(down_count_time);
        }
        let font_size = this.font_size.getComponent(EditBox).string
        if(font_size.length>0) {
            this.m_data.font_info.font_size = parseInt(font_size)
        }
        
        if(this.call_back!=null){
            this.call_back(this.m_data)
        }
    }
}


