import { _decorator, Component, instantiate, Node, Prefab, Rect, ScrollView, Size, Sprite, UITransform, Vec2, Vec3 } from 'cc';
import { dai_dao_ju_item, event_item, guo_ju_qing_dai_dao_ju, widget_item_data } from '../../../data/data';
import { widget_base } from '../widget/widget_base';
import { gameManager } from '../gameManager';
import { bag_drag_item } from './bag_drag_item';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
import { widget_info } from './widget_info';
const { ccclass, property } = _decorator;

@ccclass('dai_dao_ju_bag')
export class dai_dao_ju_bag extends Component {
    @property(Node) btn_tip:Node;
    @property(Node) btn_addtime:Node;
    @property(Node) content:Node;
    @property(Node) btn_info:Node;
    @property(Prefab) item_prefab:Prefab = null;
    @property(Node) action_node:Node = null;
    @property(widget_info) widget_info:widget_info = null;
    @property(Node) drag_des:Node = null;
    private m_tip_call_back = null;
    private m_add_time_call = null;
    private cur_select_item:bag_drag_item = null;
    private mWidgetList:Map<number,bag_drag_item> = new Map;
    private mData:guo_ju_qing_dai_dao_ju = null;
    private mScrollView:Node = null;
    public initView(data:guo_ju_qing_dai_dao_ju,tip_call,add_time_call){
        this.content.removeAllChildren()
        this.mWidgetList.clear()
        this.mData = data;
        this.btn_info.active = false;
        this.drag_des.active = false;
        ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        this.m_tip_call_back = tip_call;
        this.m_add_time_call = add_time_call;
        let list:widget_item_data[] = [];
        let page_list = gameManager.Singleton.getSceneManager().GetPageList();
        for (let index = 0; index < page_list.length; index++) {
            const element = page_list[index];
            let l = gameManager.getWidgetList(element);
            list=list.concat(l) 
        }
        let getWidgetData = (id)=>{
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                if(id==element.att.id){
                    return element;
                }
            }
            return null;
        }
        for (let index = 0; index < data.widget_list.length; index++) {
            const element:dai_dao_ju_item = data.widget_list[index];
            if(element.widget_id!=-1){
                let item = instantiate(this.item_prefab)
                item.parent = this.content
                if(element.is_in_bag){
                    item.active = true;
                }else{
                    item.active = false;
                }
                item.getComponent(bag_drag_item).initView(element,getWidgetData(element.widget_id),this.onItemClick.bind(this),this.onItemFinish.bind(this))
                this.mWidgetList.set(element.widget_id,item.getComponent(bag_drag_item))
            }
          
        }

        this.btn_tip.on(Node.EventType.TOUCH_END,()=>{
            if( this.m_tip_call_back !=null){
                this.m_tip_call_back();
            }
        })

        if(data.binding_count_time_widget!=-1){
            this.btn_addtime.active = true;
        }else{
            this.btn_addtime.active = false;
        }

        this.btn_addtime.on(Node.EventType.TOUCH_END,()=>{
            if( this.m_add_time_call !=null){
                this.m_add_time_call(this.mData.binding_count_time_widget,this.mData.add_time_number);
            }
        })

        this.btn_info.on(Node.EventType.TOUCH_END,()=>{
            if( this.cur_select_item !=null){
                this.widget_info.show(this.cur_select_item.getData().info_res)
            }
        })
        this.mScrollView = gameManager.Singleton.getCurSceneLayerPage().getBG()
    }

    protected update(dt: number): void {
        if(this.mScrollView!=null){
            this.action_node.position =  this.mScrollView.position;
        }

        if(this.cur_select_item!=null){
            if(!this.cur_select_item.isZero()){
                this.EnabledScroll(false)
            }else{
                this.EnabledScroll(true)
            }
        }
    }


    hideAllItem(){
        for (let index = 0; index < this.content.children.length; index++) {
            const element = this.content.children[index];
            element.getComponent(bag_drag_item).unSelect()
        }
    }
    onItemClick(item:bag_drag_item){
        if(this.cur_select_item==item){
            return
        }
        this.hideAllItem()
        this.cur_select_item = item;
        this.cur_select_item.onSelect()
        this.btn_info.active = true;
        this.addDragRect(this.cur_select_item.getRect())
        this.cur_select_item.registeredEvent(this.action_node)
        this.action_node.on(Node.EventType.TOUCH_START,()=>{
            this.action_node.off(Node.EventType.TOUCH_START)
            this.btn_info.active = false;
            this.cur_select_item.unSelect()
            this.cur_select_item = null;
        })
    }

    onItemFinish(item:bag_drag_item){
        this.cur_select_item = null;
        item.DeleteSelf()
        this.action_node.off(Node.EventType.TOUCH_START)
        this.btn_info.active = false;
        if(this.isFinishAll()){
            if( this.mData.finish_event_id!=-1){
                gameManager.Singleton.exeEvent(this.mData.finish_event_id)
            }
        }
    }

    isFinishAll(){
        return this.content.children.length<=0;
    }

    beActive(widgetId:number,event:event_item){
        if(event.type===config.event_type.active_event){
            let drag_item = this.mWidgetList.get(widgetId)
            if(drag_item!=null){
                drag_item.node.active = true;
            }
        }
    }

    EnabledScroll(enabled:boolean){
        this.content.parent.parent.getComponent(ScrollView).enabled = enabled;
    }

    addDragRect(r:Rect){
        // this.drag_des.position = new Vec3(r.x+r.width*0.5,r.y+r.height*0.5);
        // this.drag_des.getComponent(UITransform).setContentSize(new Size(r.width,r.height))
    }

}


