// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.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

import pg from "../pg_jj15_zww";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ResizeAuto extends cc.Component {
    @property
    r_width: number = 1
    @property
    r_height: number = 1
    @property
    r_top: number = 0
    @property
    r_bottom: number = 0
    @property
    p_left: number = 0
    @property
    p_right: number = 0
    @property
    p_top: number = 0
    @property
    p_bottom: number = 0
    @property
    noHead: number = 0
    @property
    debug: number = 0
    // LIFE-CYCLE CALLBACKS:

    private resizeScaleX;
    private resizeScaleY;
    private posX;
    private posY;
    private nodeWidth;
    private nodeHeight;
    private nodeX;
    private nodeY;
    onLoad() {
        let { scaleX, posX, scaleY, posY } = pg.screen.winBlackArea();
        this.resizeScaleX = scaleX;
        this.resizeScaleY = scaleY;
        this.posX = posX;
        this.posY = posY;
        let { width, height, x, y } = this.node;
        this.nodeWidth = width;
        this.nodeHeight = height;
        this.nodeX = x;
        this.nodeY = y;
    }

    start() {
        this.resize();
    }
    onDestroy() {
    }
    resize() {
        if (!this.resizeScaleX && !this.nodeWidth) {
            console.warn("手动调用此方法时，不能再onLoad中使用");
            return;
        }
        this.resizeHeight();
        this.resizeWidth();
    }

    //高屏幕适配
    resizeHeight() {
        if (this.resizeScaleY <= 1.05) return;
        let scaleY = this.resizeScaleY;
        let posY = this.posY;
        let nodeWidth = this.nodeWidth;
        let nodeHeight = this.nodeHeight;
        let nodeX = this.nodeX;
        let nodeY = this.nodeY;
        if (this.debug == 1) {
            pg.logger.d("断点调试点");
        }
        //宽度拉伸
        if (this.r_width == 1) {
            this.node.width = nodeWidth * scaleY;
        }
        //高度拉升
        if (this.r_height == 1) {
            this.node.height = nodeHeight * scaleY;
        }

        //动态上移
        if (this.p_top > 0) {
            if (this.p_top == 1) this.p_top = 0.5;
            this.node.y = nodeY + posY / 2 * this.p_top
        }
        //动态下移
        if (this.p_bottom > 0) {
            if (this.p_bottom == 1) this.p_bottom = 0.5;
            this.node.y = nodeY - posY / 2 * this.p_bottom
        }
    }

    //长屏幕适配
    resizeWidth() {
        if (this.resizeScaleX <= 1.05) return;
        let resizeScaleX = this.resizeScaleX;
        let posX = this.posX;
        let nodeWidth = this.nodeWidth;
        let nodeHeight = this.nodeHeight;
        let nodeX = this.nodeX;
        let nodeY = this.nodeY;
        if (this.debug == 1) {
            pg.logger.d("断点调试点");
        }
        //宽度拉伸
        if (this.r_width == 1) {
            this.node.width = nodeWidth * resizeScaleX;
        }
        //高度拉升
        if (this.r_height == 1) {
            this.node.height = nodeHeight * resizeScaleX;
            //高度拉伸后，图片顶部齐边【坐标下移】  y坐标下移
            if (this.r_top == 1) {
                this.node.y = nodeY - nodeHeight * (resizeScaleX - 1) / 2;
            }
            //高度拉伸后，图片底部齐边【坐标上移】 y坐标上移
            if (this.r_bottom == 1) {
                this.node.y = nodeY + nodeHeight * (resizeScaleX - 1) / 2;
            }
        }

        //如果是刘海屏，减去对应的刘海屏的宽度
        let lhpSize = { top: 0, bottom: 0 };
        if (!this.noHead && posX != 0) lhpSize = { top: 44 * 1.5, bottom: 34 * 1.5 };
        //动态左移   
        if (this.p_left == 1) {
            this.node.x = nodeX - posX / 2 + lhpSize.top;
        }
        //动态右移
        if (this.p_right == 1) {
            this.node.x = nodeX + posX / 2 - lhpSize.bottom;
        }
    }
}






