import { _decorator, Component, EventTouch, Label, Node, UITransform, Vec3 } from 'cc';
import { att_text_sound_data, bag_item_data } from '../../../data/data';
import { sound_item } from '../sound_item';
import { config } from '../../config';
import { control } from '../control';
import { ClientEvent } from '../../clientEvent';
import { Attributes } from '../Attributes';
const { ccclass, property } = _decorator;

@ccclass('Text_Sound')
export class Text_Sound extends Component {
    @property(sound_item) m_sound_item:sound_item = null;
    @property(Node) lab_text:Node = null;
    @property(Node) item_node:Node = null;
    private m_data:att_text_sound_data = null;
    public initView(data:att_text_sound_data){

        this.m_data = data;

        this.m_sound_item.init(false)

        if(this.m_data.sound_data===null){
            this.m_data.sound_data = new bag_item_data
            this.m_data.pos_y = this.item_node.position.y;
        }

        this.updateView(this.m_data)

        this.item_node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
            let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
            let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
            this.node.position = new Vec3(0,n_p.y);
            this.m_data.pos_y = n_p.y
            Attributes.Singleton.getTextSoundCom().update_att(this.m_data)

        })
    }

    public updateView(data:att_text_sound_data){

        console.log("Text_Sound",data)
        this.m_data = data;

        if(this.m_data.text.length<=0){

        }else{
            this.lab_text.getComponent(Label).string = this.m_data.text;
        }

        if(this.m_data.pos_y!=0){
            this.item_node.position = new Vec3(this.item_node.position.x,this.m_data.pos_y)
        }
        if(this.m_data.sound_data.url.length>0){
            this.m_sound_item.changeAudio(this.m_data.sound_data,control.mp3_cache.get(this.m_data.sound_data.name))
        }
    }
}


