import { _decorator, Button, Component, instantiate, Node, Prefab } from 'cc';
import { config } from '../../config';
import { add_animation_item } from './add_animation_item';
import { att_ani_data } from '../../../data/data';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
import { new_animation } from './new_animation';
import { edit_animation } from './edit_animation';
const { ccclass, property } = _decorator;

@ccclass('add_animation')
export class add_animation extends Component {
    @property(Node) content:Node = null;
    @property(Node) btn_close:Node = null;
    @property(Prefab) item_prefab:Prefab = null;
    @property(Node) btn_add_animation:Node = null;
    @property(Node) btn_delete_animation:Node = null;
    @property(Node) btn_edit_animation:Node = null;
    @property(Node) new_animation_node:Node = null;
    @property(Node) edit_animation_node:Node = null;
    private call_back = null;
    private m_list:att_ani_data[] = []
    private cur_select:add_animation_item = null;
    public show(list:att_ani_data[],call){
        this.call_back = call;
        this.m_list = list;
        this.updateView()
        this.btn_close.on(Node.EventType.TOUCH_END,()=>{
            this.close()
        })
        this.btn_edit_animation.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_list.length<=0){
                return tools.showToast("无动画可编辑！")
            }
            this.edit_animation_node.getComponent(edit_animation).show(this.cur_select.getData())
        })
        this.new_animation_node.getComponent(new_animation).initView(this.onCreateAni.bind(this))
        this.btn_add_animation.on(Node.EventType.TOUCH_END,()=>{
            this.new_animation_node.getComponent(new_animation).show(this.m_list.length, Attributes.Singleton.get_cur_att_data().id)
        })
        this.btn_delete_animation.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_list.length<=0){
                return tools.showToast("无动画可删除！")
            }
            tools.show_dialog("是否删除当前选中的动画？",()=>{
                this.m_list.splice(this.cur_select.getIndex(),1)
                this.cur_select.node.removeFromParent()
                if(this.m_list.length<=0){
                    this.btn_delete_animation.getComponent(Button).interactable = false;
                }else{
                    this.cur_select = this.content.children[this.content.children.length-1].getComponent(add_animation_item)
                    this.cur_select.selectStatus()
                }
                if(this.call_back!=null){
                    this.call_back(this.m_list)
                }
            })
        })
        this.btn_edit_animation.getComponent(Button).interactable = list.length<=0?false:true
        this.btn_delete_animation.getComponent(Button).interactable = list.length<=0?false:true
    }
    updateView(){
        this.content.removeAllChildren()
        for (let index = 0; index < this.m_list.length; index++) {
            const element = this.m_list[index];
            let item = instantiate(this.item_prefab)
            item.parent = this.content;
            item.getComponent(add_animation_item).initView(element,this.onItemClick.bind(this),index)
            if(index===0){
                item.getComponent(add_animation_item).selectStatus()
                this.cur_select = item.getComponent(add_animation_item);
            }
        }
    }

    onCreateAni(d:att_ani_data){
        this.m_list.push(d)
        this.updateView()
        this.btn_edit_animation.getComponent(Button).interactable = this.m_list.length<=0?false:true
        this.btn_delete_animation.getComponent(Button).interactable =  this.m_list.length<=0?false:true

        if(this.call_back!=null){
            this.call_back(this.m_list)
        }
    }
    onItemClick(item:add_animation_item){
        for (let index = 0; index < this.content.children.length; index++) {
            const element = this.content.children[index];
            element.getComponent(add_animation_item).unSelectStatus()
        }
        item.selectStatus()
        this.cur_select = item;
        this.btn_edit_animation.getComponent(Button).interactable  = true;
        this.btn_delete_animation.getComponent(Button).interactable = true
    }

    close(){
        this.node.destroy()
    }
}


