import AppFacade from "../AppFacade"
import SoundMgr from "../Manager/SoundMgr"
import GameUtil from "../Util/GameUtil"
import UIUtil from "../Util/UIUtil"
import UI from "./UI"
import { UIAnimation, UIAnimationParams } from "./UIConst"


export enum UIButtonType {
    None = 0,     //普通类型   
    SwitchImg,  //点击切换图片，从Img0-ImgN
}


//构造的参数
class UIButtonConParams {
    type?: UIButtonType=UIButtonType.None  //按钮功能类型
    maxImgNum?: number   //切换图片的最大数量
    btnName?: string     //按钮名字
    clickSound?: string  //点击按钮音效
    redKey?: string      //红点
}


export default class UIButton extends UI {

    private btn: cc.Button
    private onClickCb: Function
    private imgs: Array<cc.Sprite>
    private imgIndex: number = 0
    public set ImgIndex(val: number) {
        if (this.imgIndex == val) return
        this.imgIndex = val
        this.swicthImg(this.imgIndex)
    }
    public params: UIButtonConParams

    private redKey: string
    private redTip: cc.Node
    private label: cc.Label

    private tween: cc.Tween

    public constructor(params?: UIButtonConParams) {
        super()
        this.params=params??new UIButtonConParams()
    }

    protected async getChilds() {
        this.btn = this.getChildComp("", cc.Button)
        this.btn.node.on("click", this.onClick, this)

        if (this.params.btnName) {
            this.updateBtnName(this.params.btnName)
        }
        if (this.params.type == UIButtonType.SwitchImg) {
            this.imgs = new Array()
            if (!this.params.maxImgNum) console.error(this.node.name + "没有设置maxImgNum")
            for (let i = 0; i < this.params.maxImgNum; ++i) {
                let img = this.getChildComp("Img" + i, cc.Sprite)
                if (img) {
                    this.imgs.push(img)
                    img.node.active = i == this.imgIndex
                }
            }
        }
        this.updateRedTip(this.params?.redKey)

        if(this.params.type==UIButtonType.SwitchImg){
            this.swicthImg(this.imgIndex)
        }
    }

    protected async onUpdateData(data?: UIButtonConParams) {
        let redKey = data?.redKey || ""
        this.updateRedTip(redKey)
    }

    private updateRedTip(redKey: string) {
        if (GameUtil.isStringEmpty(redKey) || this.redKey == redKey) return
        this.redKey = redKey
        if (!this.redTip) {
            this.redTip = this.getChildNode("RedTip")
        }
        // this.redTip && RedPointController.Inst.model.registerRedPoint(redKey, this.redTip)
    }

    public addClickCb(cb: Function) {
        this.onClickCb = cb
    }

    protected onClick() {
        this.swicthImg(this.imgIndex + 1)
        let clickSound = this.params?.clickSound
        AppFacade.getMgr(SoundMgr).playEffect(clickSound)
        this.onClickCb && this.onClickCb()
    }

    private swicthImg(index: number) {
        if(!this.isLoadFinish) return
        if (this.params.type != UIButtonType.SwitchImg || index < 0) return
        let preIndex = this.imgIndex
        this.imgIndex = index % this.params.maxImgNum
        let preImg = this.imgs[preIndex]
        preImg && (preImg.node.active = false)
        let curImg = this.imgs[this.imgIndex]
        curImg && (curImg.node.active = true)
    }

    //沉默是不能点击 半透明
    public setSlience(isSlience: boolean) {
        UIUtil.setSlience(this.btn, isSlience)
        UIUtil.setSlience(this.label?.node, isSlience)
    }


    public setActive(isActive: boolean) {
        if (!this.isRootView()) return
        GameUtil.setActive(this.node, isActive)
        GameUtil.setActive(this.label?.node,isActive)
        this.btn.interactable = isActive
    }

    /**
     * 隐藏是通过修改透明度0
    */
    public setActive1(isActive: boolean) {
        if (!this.isRootView()) return
        GameUtil.setActive1(this.btn.node,isActive)
        GameUtil.setActive1(this.label?.node,isActive)
    }

    public setInteraction(use: boolean) {
        this.btn.interactable = use
    }

    /**
     * 设置过名字后才有label组件
     */
    public updateBtnName(name: string) {
        if (!this.label) {
            this.label = this.getChildComp("Background/Label", cc.Label)
        }
        this.label.string = name
    }


    public setLabelPos(pos:cc.Vec3){
        if(!this.label){
            this.label = this.getChildComp("Background/Label", cc.Label)
        }
        if(!this.label?.isValid) return
        let curParent=this.label.node.parent
        //优化了
        if(curParent!=this.node){
            let wPos=this.node.convertToWorldSpaceAR(pos)
            pos=curParent.convertToNodeSpaceAR(wPos)
        }
        this.label.node.setPosition(pos)
    }

    public playAnimation(isPlay:boolean,params?:UIAnimationParams){
        let type=params?.type
        if(!isPlay){
            if(type==UIAnimation.Scale){
                this.node.scale=1
            }else if(type==UIAnimation.Circle){
                this.node.angle=0
            }
            this.tween?.stop()
        }else{
            let time=params?.time??0.3
            if(type==UIAnimation.Scale){
                this.tween = cc.tween(this.node).to(time, { scale: 1 }).to(time, { scale: 1.05 })
                this.tween = this.tween.repeatForever(this.tween)
            }else if(type==UIAnimation.Circle){
                this.tween = cc.tween(this.node).by(time, { angle: 360 })
                this.tween = this.tween.repeatForever(this.tween)
            }
            this.tween?.start()
        }
    }
}