import AppConfig from "../Config/AppConfig";
import AppPlatform from "./AppPlatform";
import AppSwitchConfig from "../Config/AppSwitchConfig";


export default class Utilit {
    public static readonly ryw_OriginStageWidth_custom = 1334;
    public static readonly ryw_OriginStageHeight_custom = 750;


    public static readonly ryw_grayscaleMat_custom: Array<number> =
        [0.3086, 0.6094, 0.0820, 0, 0,
            0.3086, 0.6094, 0.0820, 0, 0,
            0.3086, 0.6094, 0.0820, 0, 0,
            0, 0, 0, 1, 0];



    /**
    * 获取一个随机值[0,range)
    * @param range 
    */
    public static random_custom(range: number): number {
        return Math.floor(Math.random() * range);
    }

    // /**
    //  * 将游戏中的尺寸转为设备尺寸
    //  * 常用与将游戏中自定义大小的框来放原生Banner
    //  * @param cwidth 在设计尺寸中的宽
    //  * @param bottom 在设计尺寸中距离底部的高度
    //  */
    // public static convertDesignToFrameSizeToBottom(cwidth: number = 600, bottom: number = 0) {
    //     let nodeWidth = cwidth;

    //     let size = cc.view.getFrameSize();
    //     //let size = cc.size(sw, sh);
    //     //设计尺寸
    //     let size2 = cc.view.getDesignResolutionSize();
    //     let nodex = (size2.width - nodeWidth)/2;
    //     //算出缩放比例
    //     let scale = size.width / size2.width;
    //     //屏幕尺寸与设计尺寸计算
    //     let sHeight = size2.width * size.height / size.width;
    //     //坐标系Y轴相反
    //     let pointY = sHeight - pos.y;

    //     //左上角
    //     let left = nodex * scale;
    //     //左上角
    //     let top = pointY * scale
    //     //计算节点宽的处理
    //     let width = nodeWidth * scale;

    //     let tab = {}
    //     tab["left"] = left;
    //     tab["top"] = top;
    //     tab["width"] = width;

    //     return tab;
    // }


    /**
     * 将游戏中的尺寸转为设备尺寸
     * 常用与将游戏中自定义大小的框来放原生Banner
     * @param node 接收的节点
     * @returns  返回结构体 {"left":0,"top":0,"width":0}
     */
    public static convertDesignToFrameSize_custom(node: cc.Node): any {
        //节点的坐标 将一个相对于节点左下角的坐标位置转换到世界空间坐标系。
        let pos = node.convertToWorldSpace(cc.v2(0, node.height));
        let nodeWidth = node.width;//节点宽
        let size = cc.view.getFrameSize();
        //当前实际尺寸
        let size3 = cc.view.getVisibleSize();

        let canvasNode = cc.director.getScene().getChildByName('Canvas')
        let scale = 1;
        let _cheight: number = 0;
        let _cwidth: number = 0;
        if (canvasNode) {
            let canvas = canvasNode.getComponent(cc.Canvas)
            if (canvas.fitWidth && canvas.fitHeight) {
                let scaleX = size.width / size3.width;
                let scaleY = size.height / size3.height;
                if (scaleX < scaleY) {
                    scale = scaleX;
                    _cwidth = size3.width;
                    _cheight = size.height * size3.width / size.width
                } else {
                    scale = scaleY;
                    _cheight = size3.height;
                    _cwidth = size.width * size3.height / size.height
                }
                _cheight = (_cheight - size3.height) / 2
                _cwidth = (_cwidth - size3.width) / 2
            } else if (canvas.fitWidth) {
                scale = size.width / size3.width
            } else if (canvas.fitHeight) {
                scale = size.height / size3.height
            }
        }

        //坐标系Y轴相反
        let pointY = size3.height - pos.y + _cheight;
        //左上角
        let left = pos.x * scale + _cwidth;
        //左上角
        let top = pointY * scale
        //计算节点宽的处理
        let width = nodeWidth * scale;

        let tab = {}
        tab["left"] = left;
        tab["top"] = top;
        tab["width"] = width;

        return tab;
    }

    /**
     * 颜色16进制转rgb 不带#
     * @param hex 
     */
    public static colorHex2Rgb_custom(hex: string): cc.Color {
        let color = cc.color(75, 154, 228)
        if (hex.length == 6) {
            let r = parseInt("0x" + hex.slice(0, 2))
            let g = parseInt("0x" + hex.slice(2, 4))
            let b = parseInt("0x" + hex.slice(4, 6))
            color.r = r;
            color.g = g;
            color.b = b;
        }
        return color;
    }

    public static checkVersions_custom(): boolean {
        let cur = AppConfig.Versions_custom;
        if (null === cur || "" === cur) {
            return true;
        }

        if (AppPlatform.is_TT_GAME_custom())//抖音,字节跳动
        {
            return cur === AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().ttcfg_custom.ttversions_custom;
        }
        else if (AppPlatform.is_OPPO_GAME_custom())//oppo小游戏
        {
            return cur === AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().oppocfg_custom.oppoversions_custom;
        }
        else if (AppPlatform.is_QQ_PLAY_custom())//qq小游戏
        {
            return cur === AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().qqcfg_custom.qqversions_custom;
        }
        else if (AppPlatform.is_WECHAT_GAME_custom())//微信小游戏
        {
            return cur === AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().version_custom;
        }
        else if (AppPlatform.is_WECHAT_GAME_custom()) //VIVO 小游戏
        {
            return cur === AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().vivocfg_custom.vivoversions_custom;
        }
        // else if (AppPlatform.is_BD_GAME_custom())//百度小游戏
        // {
        //     return cur === AppSwitchConfig.getInstance_custom().getAppSwitchData_custom().bdcfg_custom.version_custom;
        // }
        else {
            return true;
        }
    }
}