import { _decorator, AudioSource, Canvas, Component, EventTouch, instantiate, Node, Rect, Size, UITransform, Vec3 } from 'cc';
import { level_list } from './edit/level_list';
import { http } from './http';
import { level_list_item_data } from '../data/data';
import { tools } from './tools';
import { control } from './edit/control';
import { config } from './config';
import { ClientEvent } from './clientEvent';
import { edit_scene } from './edit/edit_scene';
import { Attributes } from './edit/Attributes';
import { base_res } from './edit/base_res';
import { widget_item } from './edit/widget_item';
import { img_item } from './edit/img_item';
const { ccclass, property } = _decorator;

@ccclass('main')
export class main extends Component {
    public static g_canvas:Canvas = null;
    public static loading_view:Node = null;
    public static g_audioSource:AudioSource = null;
    public static cur_play_audio:string = "";
    public static Singleton:main = null;
    @property(Node) level_list:Node = null;
    @property(Node) btn_back_level_list:Node = null;
    @property(control) control_view:control = null;
    @property(edit_scene) edit_scene_view:edit_scene = null;
    @property(Node) action_node:Node = null;
    @property(Node) res_action_node:Node = null;
    @property(Node) attributes_node:Node = null;
    protected start(): void {
        this.level_list.active = true;
        main.Singleton = this;
        main.g_canvas = this.node.getComponent(Canvas);
        this.level_list.getComponent(level_list).init(this.onSelectLevel.bind(this))
        this.btn_back_level_list.on(Node.EventType.TOUCH_END,()=>{
            this.onBackLevelList()
        })
        main.g_audioSource = this.node.addComponent(AudioSource)
        ClientEvent.on(config.Event.DragWidget,this.onDragWidget,this)
        ClientEvent.on(config.Event.DragRes,this.onDragRes,this)
        this.action_node.on(Node.EventType.MOUSE_MOVE,this.onMoveWidget,this)
        this.action_node.on(Node.EventType.TOUCH_END,this.onEndWidget,this)
        this.action_node.on(Node.EventType.MOUSE_UP,this.onEndWidget,this)
        this.action_node.on(Node.EventType.MOUSE_LEAVE,this.onEndWidget,this)
        this.action_node.on(Node.EventType.TOUCH_CANCEL,this.onEndWidget,this)


        this.res_action_node.on(Node.EventType.MOUSE_MOVE,this.onMoveRes,this)
        // this.res_action_node.on(Node.EventType.TOUCH_END,this.onEndRes,this)
        this.res_action_node.on(Node.EventType.MOUSE_UP,this.onEndRes,this)
        this.res_action_node.on(Node.EventType.MOUSE_LEAVE,this.onEndRes,this)
        this.res_action_node.on(Node.EventType.TOUCH_CANCEL,this.onEndRes,this)

        this.attributes_node.getComponent(Attributes).initView(this)
    }

    public static getAudioSource(){
        return main.g_audioSource;
    }

    public getEditSceneView():edit_scene {
        return this.edit_scene_view
    }
    
    onSelectLevel(data:level_list_item_data){
        this.startEdit(data.id)
    }

    add_res(data){
        let self = this;
        tools.show_loading(()=>{
            self.control_view.loading_all_resources(data,()=>{
                self.level_list.active = false;
                tools.hide_loading()
                self.control_view.init(self)
                self.edit_scene_view.init(self)
            })
        })

    }

    startEdit(id:number){
        http.post("/tool/mysnote/level_info", {"id":`${id}`},(err,data)=>{
            if(!err){
                let _data = JSON.parse(data);
                this.add_res(_data)
            }

        })
    }

    onBackLevelList(){
        this.level_list.active = true;
    }

    onDragWidget(node:Node){
        if(this.action_node.children.length<=0&&!this.action_node.active){
            this.action_node.active = true;
            let clone_node = instantiate(node)
            clone_node.parent = this.action_node;
            clone_node.getComponent(widget_item).setData(node.getComponent(widget_item).getData())
            let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
            clone_node.position = n_p;
        }
    }

    onDragRes(node:Node){

        if(this.res_action_node.children.length<=0&&!this.res_action_node.active){
            this.res_action_node.active = true;
            let clone_node = instantiate(node)
            clone_node.getComponent(base_res).setType(node.getComponent(base_res).getType())
            clone_node.getComponent(base_res).setData(node.getComponent(base_res).getData())
            clone_node.parent = this.res_action_node;
            let n_p = node.parent.getComponent(UITransform).convertToWorldSpaceAR(node.position)
            clone_node.position = n_p;
        }
    }
    onMoveWidget(Event:EventTouch){
        console.log("onMoveWidget")
        if(this.action_node.children.length>0){
            this.action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
        }

    }
    onMoveRes(Event:EventTouch){
        console.log("onMoveRes")
        if(this.res_action_node.children.length>0){
            this.res_action_node.children[0].position = new Vec3(Event.getUILocation().x,Event.getUILocation().y)
        }

    }
    onEndRes(Event:EventTouch){
        if(this.res_action_node.children.length<=0){
            return;
        }
        let rect = this.attributes_node.getComponent(Attributes).getUrlCom().getRect()
        if(rect.contains(Event.getUILocation())){
            console.log("onEndRes")
            let type = this.res_action_node.children[0].getComponent(base_res).getType()
            if(type===config.select_res_btn_type.SOUND_LIST){
                tools.showToast("类型不一致！")
            }else{
                let data = this.res_action_node.children[0].getComponent(img_item).getData()
                this.attributes_node.getComponent(Attributes).getUrlCom().update_att(data.name)
                let att = this.attributes_node.getComponent(Attributes).get_cur_att_data();
                att.src = data.url
                att.src_name = data.name
                ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,att,config.attributes_list_type.url)
            }
           
        }
        ClientEvent.dispatchEvent(config.Event.DragResEndOnCheck,Event.getUILocation(),this.res_action_node.children[0])
        this.res_action_node.removeAllChildren()
        this.res_action_node.active = false;
    }

    onEndWidget(Event:EventTouch){
        let size = new Size(this.edit_scene_view.getComponent(UITransform).contentSize.width*0.5,this.edit_scene_view.getComponent(UITransform).contentSize.height*0.5);
        let pos = this.node.getComponent(UITransform).convertToWorldSpaceAR(this.edit_scene_view.node.position);
        let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
        if(rect.contains(Event.getUILocation())){
            console.log("onEndWidget")
            ClientEvent.dispatchEvent(config.Event.DragWidgetEnd)
        }
        this.action_node.removeAllChildren()
        this.action_node.active = false;
    }

    IsHaveDragActionRes(){
        return this.res_action_node.children.length>0
    }
}


