import {
    RandomInt
} from "../script/utils";

cc.Class({
    extends: cc.Component,

    properties: {

    },

    start() {
        this._textureIdMapDataContainer = {};

        const sprite = cc.find('Canvas/bg_biga');
        sprite.on(cc.Node.EventType.TOUCH_START, () => {
            sprite.color = cc.color(RandomInt(255), RandomInt(255), RandomInt(255));
        });
        sprite._hitTest = this._hitTest.bind(this);

        const sprite2 = cc.find('Canvas/bg_biga2');
        sprite2.on(cc.Node.EventType.TOUCH_START, () => {
            sprite2.color = cc.color(RandomInt(255), RandomInt(255), RandomInt(255));
        });
        sprite2._hitTest = this._hitTest.bind(this);
    },

    _hitTest(location, listener) {
        const sprite = listener.owner;

        let spriteFrame = sprite.getComponent(cc.Sprite).spriteFrame;
        if (spriteFrame == null) {
            return false;
        }
        let posInNode = sprite.convertToNodeSpaceAR(location);
        let rect = spriteFrame.getRect();
        let offset = spriteFrame.getOffset();

        if ((posInNode.x < offset.x - rect.width / 2)
            || (posInNode.y < offset.y - rect.height / 2)
            || (posInNode.x > (offset.x + rect.width / 2))
            || (posInNode.y > (offset.y + rect.height / 2))) {
            return false;
        } else {
            let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2))

            let tex = spriteFrame.getTexture();
            let data;
            if (tex instanceof cc.RenderTexture) { // 细图(width <= 512 && height <= 512) 被引擎自动打包了
                if (cc.sys.platform === cc.sys.QQ_PLAY) { // 玩一玩平台
                    throw "在玩一玩平台，请确保你的SpriteFrame 的宽高至少有一个大于512, 这样不会被Atlas, 不然被引擎自动Atlas之后，因为玩一玩不支持gl.readPixels 或 getImageData这样的接口，像素就读不出来了"
                }

                var buffer = new Uint8Array(4);
                // data就是这个texture的rgba值数组
                if (spriteFrame.isRotated()) {
                    data = tex.readPixels(buffer, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1);
                } else {
                    data = tex.readPixels(buffer, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1);
                }
                console.log(tex);
            } else {
                var dataContainer = this._textureIdMapDataContainer[tex.getId()];

                if (cc.sys.platform === cc.sys.QQ_PLAY) { // 针对玩一玩的特殊方式
                    if (!dataContainer) {
                        tex.getHtmlElementObj().bkImage || tex.getHtmlElementObj()._generateBKImage();
                        dataContainer = tex.getHtmlElementObj().bkImage;
                        this._textureIdMapDataContainer[tex.getId()] = dataContainer;
                    }

                    var buffer = dataContainer.buffer;
                    buffer.rewind();

                    if (spriteFrame.isRotated()) {
                        buffer.jumpBytes(((rect.x + posInRect.y) + (rect.y + posInRect.x) * tex.width) * 4);
                    } else {
                        buffer.jumpBytes(((rect.x + posInRect.x) + (rect.y + rect.height - posInRect.y) * tex.width) * 4);
                    }

                    data = [buffer.readUint8Buffer(), buffer.readUint8Buffer(), buffer.readUint8Buffer(), buffer.readUint8Buffer()];
                } else {
                    //Canvas 方式
                    var scale = 8;
                    var cvs = dataContainer;
                    if (!cvs) {
                        cvs = document.createElement("canvas");
                        var ctx = cvs.getContext('2d');
                        cvs.width = tex.width;
                        cvs.height = tex.height;
                        ctx.drawImage(tex.getHtmlElementObj(), 0, 0, tex.width, tex.height, 0, 0, tex.width / scale, tex.height / scale);
                        this._textureIdMapDataContainer[tex.getId()] = cvs;
                    }

                    var ctx = cvs.getContext('2d');
                    if (spriteFrame.isRotated()) {
                        data = ctx.getImageData((rect.x + posInRect.y) / scale, (rect.y + posInRect.x) / scale, 1, 1).data;
                    } else {
                        data = ctx.getImageData((rect.x + posInRect.x) / scale, (rect.y + rect.height - posInRect.y) / scale, 1, 1).data;
                    }
                }
            }

            if (data[3] <= 0) {
                return false;
            } else {
                return true;
            }
        }
    },

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