import { _decorator, Component, Node } from 'cc';
import { interact_page_data, ui_att_item } from '../../../data/data';
import { view_ui_att_item } from './view_ui_att_item';
import { tools } from '../../tools';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('view_interact_page')
export class view_interact_page extends Component {
    @property(Node) bg:Node = null;
    @property(Node) btn_interact_left:Node = null;
    @property(Node) btn_interact_right:Node = null;
    @property(Node) img_item_interact_bg:Node = null;
    @property(Node) btn_sure:Node = null;
    @property(Node) img_item_interact:Node = null;
    private m_data:interact_page_data = null;
    private m_page = 0;
    protected start(): void {
        this.btn_interact_left.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_data.img_item_interact_list.length>0){
                if( (this.m_page-1)<0){
                    this.m_page = this.m_data.img_item_interact_list.length-1;
                }else{
                    this.m_page-=1;
                }
                this.update_func()
            }else{
                tools.showToast("没有添加多页")
            }

        })

        this.btn_interact_right.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_data.img_item_interact_list.length>0){
                if( (this.m_page+1)>=this.m_data.img_item_interact_list.length){
                    this.m_page = 0;
                }else{
                    this.m_page+=1;
                }

                this.update_func()
            }else{
                tools.showToast("没有添加多页")
            }

        })
    }
    public updateView(data:interact_page_data){

        if(data===null){
            data = new interact_page_data;
        }


        this.m_page = 0;
        this.m_data = data;
        this.bg.getComponent(view_ui_att_item).updateAtt(data.bg)
        data.bg = this.bg.getComponent(view_ui_att_item).getData()

        this.btn_interact_left.getComponent(view_ui_att_item).updateAtt(data.btn_interact_left)
        data.btn_interact_left = this.btn_interact_left.getComponent(view_ui_att_item).getData()

        this.btn_interact_right.getComponent(view_ui_att_item).updateAtt(data.btn_interact_right)
        data.btn_interact_right = this.btn_interact_right.getComponent(view_ui_att_item).getData()

        this.img_item_interact_bg.getComponent(view_ui_att_item).updateAtt(data.img_item_interact_bg)
        data.img_item_interact_bg = this.img_item_interact_bg.getComponent(view_ui_att_item).getData()

        this.btn_sure.getComponent(view_ui_att_item).updateAtt(data.btn_sure)
        data.btn_sure = this.btn_sure.getComponent(view_ui_att_item).getData()
        if(data.img_item_interact_list.length<=0){
            this.m_data.img_item_interact_list.push(null)
        }
        this.update_func()
   
    }

    update_func = ()=>{
        this.img_item_interact.getComponent(view_ui_att_item).updateAtt(this.m_data.img_item_interact_list[this.m_page],config.update_type.update_img)
        this.m_data.img_item_interact_list[this.m_page] = this.img_item_interact.getComponent(view_ui_att_item).getData()
        if(this.m_data.img_item_interact_list[0]!=null){
            this.img_item_interact.getComponent(view_ui_att_item).updateAtt(this.m_data.img_item_interact_list[0],config.update_type.update_info)
        }
    }

    public getData(){
        return this.m_data;
    }
}


