
var sprite = function (uncheckedImg, checkedImg, spriteButtonImage, spriteButtonText, execute) {
    this.UncheckedImg = uncheckedImg;
    this.CheckedImg = checkedImg;
    this.SpriteButtonImage = spriteButtonImage;
    this.SpriteButtonText = spriteButtonText;

    this.display = function () {
        this.SpriteButtonImage.visible = true;
        this.SpriteButtonText.visible = true;
    };

    this.isVisible = function(){
        return this.SpriteButtonImage.visible;
    };

    this.hide = function () {
        this.SpriteButtonImage.visible = false;
        this.SpriteButtonText.visible = false;
    };

    this.checked = function () {
        EventCenter.emit("SelectedSpriteChanged", this);
        this.SpriteButtonImage.material.map = this.CheckedImg;
        execute();
    };

    this.hightLight = function () {
        this.SpriteButtonImage.material.map = this.CheckedImg;
    };

    this.unchecked = function () {
        this.SpriteButtonImage.material.map = this.UncheckedImg;
    };
};

var SpriteManager = {
    spriteMaxCount: 7,
    spriteInterval: 20,
    spriteRotateL: undefined,
    spriteRotateR: undefined,
    spriteQuickClip: undefined,
    spriteMove:undefined,
    spriteClip:undefined,
    isSpriteButtonAdded: false,
    spriteAreaWidth: undefined,
    spriteWidth: undefined,
    spriteHeight: undefined,
    clipType: undefined,
    allowClip: false,
    sprites:[],
    hightLightSprite:undefined,
    selectedSprite:undefined,
    infoHideTask: undefined,
    quickClipExecute: false,
    init: function () {
        this.initSpriteSize();
        EventCenter.addEventHandler("SelectedSpriteChanged", this.selectSpriteChanged);
        this.spriteMove = this.createSprite("3DWeb/ButtonImage/move.png", "3DWeb/ButtonImage/moveMouseDown.png", this.moveClipPlaneChecked, languageContent.Move);
        this.spriteClip = this.createSprite("3DWeb/ButtonImage/clip.png", "3DWeb/ButtonImage/clipMouseDown.png", this.drawLineChecked, languageContent.Clip);
        this.spriteQuickClip = this.createSprite("3DWeb/ButtonImage/AI.png", "3DWeb/ButtonImage/AIDown.png", this.quickClipChecked, languageContent.QuickClip);
        this.createSprite("3DWeb/ButtonImage/reset.png", "3DWeb/ButtonImage/resetMouseDown.png", this.resetChecked, languageContent.Reset);
        this.createSprite("3DWeb/ButtonImage/delete.png", "3DWeb/ButtonImage/deleteMouseDown.png", this.deleteChecked, languageContent.Delete);
        this.spriteRotateR = this.createSprite("3DWeb/ButtonImage/rotate-.png", "3DWeb/ButtonImage/rotate-MouseDown.png", this.rotateRChecked, languageContent.RotateR);
        this.spriteRotateL = this.createSprite("3DWeb/ButtonImage/rotate+.png", "3DWeb/ButtonImage/rotate+MouseDown.png", this.rotateLChecked, languageContent.RotateL);
    },

    initSpriteSize: function () {
        let windowWidth = window.innerWidth < OSHelper.windowWidthDefault ? OSHelper.windowWidthDefault : window.innerWidth;
        this.spriteAreaWidth = OSHelper.isPC ? windowWidth * 0.4 : windowWidth;
        this.spriteWidth = this.spriteAreaWidth / this.spriteMaxCount - this.spriteInterval;
        this.spriteHeight = this.spriteWidth;
    },

    selectSpriteChanged: function (selectItem) {
        if (SpriteManager.selectedSprite != undefined) {
            SpriteManager.selectedSprite.unchecked();
        }

        SpriteManager.selectedSprite = selectItem;
    },

    createSprite: function (originalImage, clickImage, execute, name) {
        let checkedTexture = LoaderUtility.loadTexture(originalImage);
        let uncheckedTexture = LoaderUtility.loadTexture(clickImage);
        let material = new THREE.SpriteMaterial({ map: checkedTexture });
        let spriteImage = new THREE.Sprite(material);
        spriteImage.scale.set(this.spriteWidth, this.spriteHeight, 1);
        let spriteText = this.createSpriteText(name);
        let info = new sprite(checkedTexture, uncheckedTexture, spriteImage, spriteText, execute);
        sceneOrtho.add(info.SpriteButtonImage);
        sceneOrtho.add(info.SpriteButtonText);
        info.hide();
        this.sprites.push(info);
        return info;
    },
    createSpriteText: function (text) {
        let canvas = document.createElement("canvas");
        canvas.width = 512;
        canvas.height = 512;
        let textCanvas = canvas.getContext("2d");
        textCanvas.textAlign = "center";
        textCanvas.fillStyle = "#FFF";
        textCanvas.font = "800 132px Microsoft YaHei";
        textCanvas.lineWidth = 1;
        textCanvas.fillText(text, 256, 128);
        let texture = new THREE.Texture(canvas);
        texture.needsUpdate = true;
        let material = new THREE.SpriteMaterial({ map: texture });
        let textObj = new THREE.Sprite(material);

        let size = this.spriteWidth + this.spriteInterval / 2;
        textObj.scale.set(size, size, 1);
        return textObj;
    },

    mdlFileLoaded: function (sourceenum, docarotidaiclip) {
        var hasAIClip = sourceenum == "Carotid" && docarotidaiclip=="True";
        
        if (!hasAIClip) {
            this.sprites.splice(2, 1);
        }
        this.resetSpritePosition();
        this.sprites.forEach(m => {
            if (m != this.spriteRotateL && m != this.spriteRotateR) {
                m.display();
            }
        });

        this.sprites[0].checked();
        this.allowClip = true;
        //hide loading image
        var img = document.getElementById("image");
        img.style.display = "none";
    },
    waitMdlFileLoading: function () {
        this.sprites.forEach(m => {
            m.hide();
        });

        //show loading image
        var img = document.getElementById("image");
        img.style.display = "block";
    },
    resetSpritePosition: function () {
        let spriteCount = this.sprites.length;

        if(!this.spriteRotateR.isVisible())
        {
            spriteCount -= 2;
        }

        for (let i = 0; i < spriteCount; i++) {
            let positionX = (0.5 + i - spriteCount / 2) * (this.spriteWidth + this.spriteInterval);
            let positionY = window.innerHeight / 2 - 50;
            this.sprites[i].SpriteButtonImage.position.set(positionX, positionY, 1);
            this.sprites[i].SpriteButtonText.position.set(positionX, positionY - this.spriteHeight - 10, 1);
        }
    },
    showRotateSprite: function () {
        if(!this.spriteRotateR.isVisible())
        {
            this.spriteRotateR.display();
            this.spriteRotateL.display();
            this.resetSpritePosition();
        }
    },
    hideRotateSprite: function () {
        if(this.spriteRotateR.isVisible()) {
            this.spriteRotateR.hide();
            this.spriteRotateL.hide();
            this.resetSpritePosition();
        }
    },
    mouseDown: function (rayCaster) {
        let intersectSprite = this.getIntersectSprite(rayCaster);
        if (intersectSprite != undefined) {
            intersectSprite.checked();
            document.addEventListener(OSHelper.touchEnd, this.mouseUp, false);
            return true;
        }
        return false;
    },
    mouseUp: function()
    {
        if(SpriteManager.clipType == ClipType.DrawLine)
        {
            SpriteManager.spriteClip.checked();
        }

        if(SpriteManager.clipType == ClipType.MoveClipPlane)
        {
            SpriteManager.spriteMove.checked();
        }

        document.removeEventListener(OSHelper.touchEnd, this.mouseUp, false);
        document.body.style.cursor = 'default';
    },
    mouseMove: function (rayCaster) {
        if (this.hightLightSprite != undefined && this.hightLightSprite != this.selectedSprite) {
            let intersects = rayCaster.intersectObject(this.hightLightSprite.SpriteButtonImage);
            if (intersects.length > 0) {
                return;
            } else {
                this.hightLightSprite.unchecked();
                document.body.style.cursor = 'default';
                this.hightLightSprite = undefined;
            }
        }

        let intersectSprite = this.getIntersectSprite(rayCaster);
        if (intersectSprite != undefined) {
            if (this.hightLightSprite == intersectSprite)
                return;

            this.hightLightSprite = intersectSprite;
            if (this.hightLightSprite != this.selectedSprite) {
                document.body.style.cursor = 'pointer';
                intersectSprite.hightLight();
            }
        }
    },
    getIntersectSprite: function (rayCaster) {
        let intersects;
        for (let i = 0; i < this.sprites.length; i++) {
            intersects = rayCaster.intersectObject(this.sprites[i].SpriteButtonImage);
            if (intersects.length > 0) {
                return this.sprites[i];
            }
        }
        return undefined;
    },

    rotateLChecked: function () {
        addRotateTask(-1);
    },
    rotateRChecked: function () {
        addRotateTask(1);
    },
    resetChecked: function () {
        ClipPlaneManager.resetClipInfo();
        resetCubePosition();
        ClipPlaneManager.meshIsInactive();
    },
    quickClipChecked: function () {
        if (!SpriteManager.quickClipExecute) {
            SpriteManager.quickClipExecute = true;

            var success = SpriteManager.executeQuickClip();
            if (!success) {
                //show info
                var info = document.getElementById("info");
                info.style.display = "block";
                SpriteManager.infoHideTask = setInterval(function () {
                    var info = document.getElementById("info");
                    info.style.display = "none";
                    clearInterval(SpriteManager.infoHideTask);
                }, 5000);
                // jsObj.AIClipStateCallBack(success);
                SpriteManager.quickClipExecute = false;
            }
            else {
                ClipPlaneManager.meshIsActive();
            }

        }


    },
    executeQuickClip: function () {
        // let json = jsObj.GetVesselClipPlanePoints();
        let json = Dart_GetClipPlaneData();
        let clipPlaneData = JSON.parse(json);
        if (clipPlaneData.ErrorCode != 1000) {
            return false;
        }
        convertBackToWorldPoints(clipPlaneData.WorldPoints);
        let verticesPoints = [];
        clipPlaneData.WorldPoints.forEach(item => verticesPoints.push(new THREE.Vector3(item.X, item.Y, item.Z)));
        return ClipPlaneManager.generateClipPlaneByPoints(verticesPoints,
            function () {
                SpriteManager.quickClipExecute = false;
            });
    },
    deleteChecked: function () {
        ClipPlaneManager.removeClipPlane();
        removeRotateInfo();
        LinesDrawingManager.hideClipLine();
        LinesDrawingManager.hideAll();
        ClipPlaneManager.meshIsInactive();
    },
    drawLineChecked: function () {
        SpriteManager.clipType = ClipType.DrawLine;
    },
    moveClipPlaneChecked: function () {
        SpriteManager.clipType = ClipType.MoveClipPlane;
    }
};
