import { _decorator, Component, Node, Size, Sprite, UITransform, Vec3 } from 'cc';
import { ui_base } from './ui_base';
import { interact_page_data } from '../../../data/data';
import { tools } from '../../tools';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('ui_interact_page')
export class ui_interact_page extends ui_base {
    @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 mInteractPageData:interact_page_data = null;
    private m_page = 0;
    protected init(): void {
        this.mInteractPageData = this.mTopData._interact_page_data;
        if(this.mInteractPageData===null){
            return tools.showToast("设置ui分页错误")
        }
        this.initPageView()
    }

    initPageView(){

        this.btn_interact_left.on(Node.EventType.TOUCH_END,()=>{
            if(this.mInteractPageData.img_item_interact_list.length>0){
                if( (this.m_page-1)<0){
                    this.m_page = this.mInteractPageData.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.mInteractPageData.img_item_interact_list.length>0){
                if( (this.m_page+1)>=this.mInteractPageData.img_item_interact_list.length){
                    this.m_page = 0;
                }else{
                    this.m_page+=1;
                }

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

        })

        this.btn_sure.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_page==this.mInteractPageData.answer){
                this.onFinishEvent()
                tools.showToast("成功")
            }else{
                this.onFialEvent()
                tools.showToast("失败")
                console.log("onFialEvent",this.mInteractPageData.answer,this.m_page)
            }
        })

        gameManager.initUiBaseAtt(this.img_item_interact_bg,this.mInteractPageData.img_item_interact_bg)

        gameManager.initUiBaseAtt(this.btn_interact_right,this.mInteractPageData.btn_interact_right)

        gameManager.initUiBaseAtt(this.btn_interact_left,this.mInteractPageData.btn_interact_left)

        gameManager.initUiBaseAtt(this.btn_sure,this.mInteractPageData.btn_sure)

        this.loadBg(this.mInteractPageData.bg)
        this.update_func()
    }

    update_func(){
        let curSelectItem = this.mInteractPageData.img_item_interact_list[this.m_page];
        let att = this.mInteractPageData.img_item_interact_list[0];
        this.img_item_interact.getComponent(UITransform).setContentSize(new Size(att.width,att.height))
        this.img_item_interact.position = new Vec3(att.x,att.y);
        this.img_item_interact.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(curSelectItem.res)
    }
}


