import { _decorator, assetManager, Component, ImageAsset, Node, ScrollView, Size, Sprite, SpriteFrame, Texture2D, Toggle, UITransform, Vec2 } from 'cc';
import { attributes_data, scene_item_data } from '../../data/data';
import { config } from '../config';
import { ClientEvent } from '../clientEvent';
import { tools } from '../tools';
const { ccclass, property } = _decorator;

@ccclass('scroll_scene')
export class scroll_scene extends Component {
    @property(Node) mask_view:Node = null;
    @property(Node) mask_view_content:Node = null;
    @property(Node) mask_check_node:Node = null;
    @property(Node) mask_check:Node = null;
    @property(Node) mask:Node = null;
    @property(SpriteFrame) mask_sf:SpriteFrame = null;
    private m_data:scene_item_data = null;
    private m_att:attributes_data = null;
    private _bg:Node = null;
    private m_call_back = null;
    protected start(): void {
        if(this._bg===null){
            this.setBg(this.node)
        }
        ClientEvent.on(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
    }
    UpdateAttributesToView(data:attributes_data,update_type:string){
        if(data.id===this.m_att.id){
            switch(update_type){
                case config.attributes_list_type.pos:

                    break;
                case config.attributes_list_type.animation:
                    this.m_att.animation_list = data.animation_list;
                break;
                case config.attributes_list_type.size:
                    this.m_att.width = data.width;
                    this.m_att.height = data.height;
                    this.getBg().getComponent(UITransform).contentSize = new Size(data.width,data.height)
                    break;
                case config.attributes_list_type.anchor:
                    this.m_att.anchor_x = data.anchor_x;
                    this.m_att.anchor_y = data.anchor_y;
                    this.getBg().getComponent(UITransform).setAnchorPoint(data.anchor_x, data.anchor_y)
                    break;
                case config.attributes_list_type.rotation:
                    this.m_att.rotation = data.rotation;
                    this.getBg().angle = data.rotation;
                    break;
                case config.attributes_list_type.url:
                    this.m_att.src = data.src
                    this.m_att.src_name = data.src_name
                    if(this.m_att.src.length<=0){
                        this.getBg().getComponent(Sprite).spriteFrame =null;
                    }else{
                        assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
                            if (!err && imageAsset2) {        
                                const texture = new Texture2D();
                                texture.image = imageAsset2;
                                let spFrame2 = new SpriteFrame();     
                                spFrame2.texture = texture;
                                this.getBg().getComponent(UITransform).contentSize = new Size(data.width,data.height)
                                this.getBg().getComponent(Sprite).spriteFrame = spFrame2
                            }
                        }); 
                    }
      
                    break;
                case config.attributes_list_type.is_interaction:
                    this.node.getComponent(ScrollView).enabled = this.m_att.is_interaction;
                break;
                

            }
        }
    }

    protected onDestroy(): void {
        ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
    }
    public setBg(bg:Node){
        this._bg = bg;
    }
    public getBg(){
        if(this._bg===null){
            this.setBg(this.node)
        }
        return this._bg;
    }

    public addWidget(node:Node){
        node.parent = this.getBg()
    }

    public initView(is_mask:boolean,att_data:attributes_data=null){
        if(att_data!=null){
            this.m_att =att_data;
            if(this.m_att.src.length>0){
                assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
                    if (!err && imageAsset2) {        
                        const texture = new Texture2D();
                        texture.image = imageAsset2;
                        let spFrame2 = new SpriteFrame();     
                        spFrame2.texture = texture;
                        this.getBg().getComponent(UITransform).contentSize = new Size(this.m_data.att.width,this.m_data.att.height)
                        this.getBg().getComponent(Sprite).spriteFrame = spFrame2
                    }
                }); 
            }
        }else{
            this.m_att.id = config.getNewId();
        }
        if(is_mask){
            this.mask_check_node.active = true;
            this.mask_check.getComponent(Toggle).isChecked = true;
        }else{
            this.mask_check.getComponent(Toggle).isChecked = false;
            this.mask_check_node.active = false;
        }
        this.mask_check.on(Node.EventType.TOUCH_START,()=>{
            this.mask_check.getComponent(Toggle).isChecked=!this.mask_check.getComponent(Toggle).isChecked
            this.updatStatus()
        })
        this.updatStatus()
        this.setBg(this.mask_view_content)
    }

    public initFullView(att_data:attributes_data=null){
        if(att_data!=null){
            this.m_att =att_data;
            if(this.m_att.src.length>0){
                assetManager.loadRemote<ImageAsset>(this.m_att.src, (err, imageAsset2)=>{
                    if (!err && imageAsset2) {        
                        const texture = new Texture2D();
                        texture.image = imageAsset2;
                        let spFrame2 = new SpriteFrame();     
                        spFrame2.texture = texture;
                        this.getBg().getComponent(UITransform).contentSize = new Size(this.m_data.att.width,this.m_data.att.height)
                        this.getBg().getComponent(Sprite).spriteFrame = spFrame2
                    }
                }); 
            }
        }else{
            this.m_att.id = config.getNewId();
        }
    }

    public setData(data:scene_item_data){
        this.m_data = data;
        this.m_data.att = this.m_att;
    }
    public getData(){
        return this.m_data;
    }
    public setCallBack(call){
        this.m_call_back = call;
        this.getBg().on(Node.EventType.TOUCH_START,()=>{
            if(this.m_call_back!=null){
                this.m_call_back()
            }
        })
    }

    public getScenePageAtt(){
        if(this.m_att.height===0||this.m_att.width===0){ 
            this.m_att.name = this.m_data.name!=""?this.m_data.name:"场景";
            this.m_att.height = this.getBg().getComponent(UITransform).contentSize.height;
            this.m_att.width = this.getBg().getComponent(UITransform).contentSize.width;
            this.m_att.rotation = this.getBg().angle;
            this.m_att.type = config.attributes_type.scene;
            this.m_att.src_name = this.m_att.src.length>0? this.m_att.src_name:"空";
        }
        return this.m_att;
    }

    updatStatus(){
        if(this.mask_check.getComponent(Toggle).isChecked){
            this.mask.active = true; 
        }else{
            this.mask.active = false; 
        }
    }

    public stopTouch(){
        if(this.mask_view!=null){
            this.node.getComponent(ScrollView).enabled = false;
        }
    }
    public startTouch(){
        if(this.mask_view!=null){
            if(this.m_att.is_interaction!=false){
                this.node.getComponent(ScrollView).enabled = true;
            }
        }
    }
    public getContent():Node{
        return this.node.getComponent(ScrollView).content
    }

    getAtt(){
        return this.m_att;
    }
}


