// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        relativeNode: {
            default: null,
            type: cc.Node
        },
        percentX: {
            default: 0,
            type: cc.Float 
        },
        percentY: {
            default: 0,
            type: cc.Float 
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        if (!this.relativeNode) {
            this.relativeNode = cc.find('Canvas')
        }
        this.setScale();
    },

    getNodeBoundingBox(node) {
        const box = node.getBoundingBox();
        const ration = cc.view.getDevicePixelRatio();
        const left = box.width * (node.anchorX - 1);
        const right = box.width * (1 - node.anchorX);
        const top = box.height * (1 - node.anchorY);
        const bottom = box.height * (node.anchorY - 1);
        return {left, right, top, bottom}
    },

    setScale() {
        window.ttt = this
        const {x: scaleX, y: scaleY, z: scaleZ} = this.node.getWorldScale({})
        
        const {width: cw, height: ch} = cc.view.getCanvasSize();
        const {width: dw, height: dh} = cc.view.getDesignResolutionSize();
        if ( dw /  dh > cw / ch ) {
            this.node.y = this.node.height / 2  * (1 - this.node.scale)
        } else {
            this.node.x = this.node.width / 2  * (this.node.scale - 1 )
        }
        
        // const nx = this.node.width * (1 - this.node.anchorX) - nodePos.x;
        // const ny = this.node.height * (this.node.anchorY - 1) - nodePos.y;


    }

    // update (dt) {},
});
