import { _decorator, CCInteger, CCString, Component, Node, Sprite, SpriteFrame, input, Input, __private, NodeEventType, EventTouch, EventMouse, UITransform, v2 } from 'cc';
import { BaseSpriteFrame } from './BaseSpriteFrame';
const { ccclass, property } = _decorator;

@ccclass('BaseButton')
export class BaseButton extends Component {

    @property({ type: CCString, displayName: 'img路径', tooltip: "img路径" })
    ImgPath: string;

    @property({ type: [CCInteger], displayName: '普通img编号', tooltip: "普通img编号" })
    NormalImgIndex: number;

    @property({ type: [CCInteger], displayName: '悬停img编号', tooltip: "悬停img编号" })
    HoverImgIndex: number;

    @property({ type: [CCInteger], displayName: '按下img编号', tooltip: "按下img编号" })
    PressImgIndex: number;

    @property({ type: [CCInteger], displayName: '失效img编号', tooltip: "失效img编号" })
    DisableImgIndex: number;

    //普通精灵帧
    NormalImgSpriteFrame: SpriteFrame;

    //悬停精灵帧
    HoverImgSpriteFrame: SpriteFrame;

    //按下精灵帧
    PressImgSpriteFrame: SpriteFrame;

    //失效精灵帧
    DisableImgSpriteFrame: SpriteFrame;

    //精灵对象
    SpriteObj: Sprite;

    //初始化状态
    InitState = false;

    //按钮状态
    ButtonState = 0; // 0 普通 1悬停 2按下 8失效

    start() {

        //判断是否有精灵 如果没有 就给他搞一个
        if (this.node.getComponent(Sprite))
            this.SpriteObj = this.node.getComponent(Sprite);
        else
            this.SpriteObj = this.node.addComponent(Sprite);

        //设置节点锚点为左上角
        this.node.getComponent(UITransform).anchorPoint = v2(0, 1);
        //设置类型
        this.SpriteObj.sizeMode = Sprite.SizeMode.RAW;
        //设置
        this.SpriteObj.trim = false;

        new BaseSpriteFrame(this.ImgPath, this.NormalImgIndex, _SpriteFrame => { this.NormalImgSpriteFrame = _SpriteFrame });
        new BaseSpriteFrame(this.ImgPath, this.HoverImgIndex, _SpriteFrame => { this.HoverImgSpriteFrame = _SpriteFrame });
        new BaseSpriteFrame(this.ImgPath, this.PressImgIndex, _SpriteFrame => { this.PressImgSpriteFrame = _SpriteFrame });
        new BaseSpriteFrame(this.ImgPath, this.DisableImgIndex, _SpriteFrame => { this.DisableImgSpriteFrame = _SpriteFrame });

    }

    UploadSpriteFrame(_SpriteFrame) {
        if (this.SpriteObj.spriteFrame != _SpriteFrame) {
            this.SpriteObj.spriteFrame = _SpriteFrame;
        }
    }

    update(deltaTime: number) {
        if (!this.InitState) {
            if (this.NormalImgSpriteFrame && this.HoverImgSpriteFrame && this.PressImgSpriteFrame) {
                this.InitState = true;
                this.UploadSpriteFrame(this.NormalImgSpriteFrame);

                // this.node.on(Input.EventType.TOUCH_START, this.OnPress, this);
                // this.node.on(Input.EventType.TOUCH_END, this.OnEnd, this);
                this.node.on(Node.EventType.MOUSE_ENTER, this.OnHever, this);
                this.node.on(Node.EventType.MOUSE_LEAVE, this.OffHever, this);
                this.node.on(Node.EventType.MOUSE_DOWN, this.OnPress, this);
                this.node.on(Node.EventType.MOUSE_UP, this.OnEnd, this);
            }
        } else {
            switch (this.ButtonState) {
                case 0:
                    this.UploadSpriteFrame(this.NormalImgSpriteFrame);
                    break;
                case 1:
                    this.UploadSpriteFrame(this.HoverImgSpriteFrame);
                    break;
                case 2:
                    this.UploadSpriteFrame(this.PressImgSpriteFrame);
                    break;
                case 8:
                    this.UploadSpriteFrame(this.DisableImgSpriteFrame);
                    break;
                default:
                    break;
            }
        }
    }

    OffHever(event) {
        this.ButtonState = 0;
        // this.UploadSpriteFrame(this.NormalImgSpriteFrame);
    }
    OnHever(event) {
        this.ButtonState = 1;
        // this.UploadSpriteFrame(this.HoverImgSpriteFrame);
    }
    OnEnd(event: EventTouch) {
        this.ButtonState = 0;
        // this.UploadSpriteFrame(this.NormalImgSpriteFrame);
    }

    OnPress(event: EventMouse) {
        //必须是鼠标左键
        if (event.getButton() != 0) return;


        this.ButtonState = 2;
        // this.UploadSpriteFrame(this.PressImgSpriteFrame);
    }
}


