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

// 参考： 
// https://forum.cocos.org/t/creator/38530/56
// https://forum.cocos.org/t/topic/116551

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 * sprite.anchorX))
            || (posInNode.y < offset.y - (rect.height * sprite.anchorY))
            || (posInNode.x > (offset.x + (rect.width * (1 - sprite.anchorX))))
            || (posInNode.y > (offset.y + (rect.height * (1 - sprite.anchorY))))) {
            return false;
        }

        if (!this.camera) {
            const checkCameraNode = cc.find('Canvas/CheckCamera');
            if (!checkCameraNode) {
                throw '找不到[Canvas/CheckCamera]。';
            }
            this.camera = checkCameraNode.getComponent(cc.Camera);
        }
        if (this.camera.cullingMask != -2) {
            console.warn(`[${this.camera.cullingMask}]: 检查CheckCamera的cullingMask值，应该只选择camera`);
        }
        this.camera.node.active = true;
        let posInRect = cc.v2(
            parseInt(posInNode.x - offset.x + rect.width * sprite.anchorX),
            parseInt(posInNode.y - offset.y + rect.height * sprite.anchorY)
        );
        let tex = spriteFrame.getTexture();
        this.camera.enable = true;
        let rt;
        let destory = false;
        if (tex instanceof cc.RenderTexture) {
            rt = tex;
        } else {
            rt = new cc.RenderTexture()
            rt.initWithSize(tex.width, tex.height, cc.gfx.RB_FMT_S8);
            rt.drawTextureAt(tex, 0, 0);
            destory = true;
        }
        // data就是这个texture的rgba值数组
        this.camera.targetTexture = rt;
        this.camera.render(undefined);
        let data;
        //图集里的图片可能会旋转
        if (spriteFrame.isRotated()) {
            data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1);
        } else {
            data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1);
        }
        this.camera.targetTexture = null;
        this.camera.enable = false;
        this.camera.node.active = false;

        if (destory) {
            rt.destroy();
        }

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