//
//  gateServer.js
//
//  Created by Rebecca Stankus on 3/6/18.
//  Copyright 2018 High Fidelity, Inc.
//
//  Distributed under the Apache License, Version 2.0.
//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

(function() { 
    var _this;

    var MOVEMENT_INCREMENT = 0.01;
    var MOVEMENT_INCREMENT_LOWER = 0.025;
    var MOVEMENT_INTERVAL_MS = 25;
    var MOVEMENT_INTERVAL_LOWER_MS = 5;

    var checking;
    var fullyOpen = false;
    var fullyClosed = true;
    var moving;

    var Gate = function() {
        _this = this;
    };
    print("hello");
    Gate.prototype = {
        remotelyCallable: ['raiseGate', 'lowerGate', 'openGate', 'closeGate', 'stopMovement'],
        preload: function(entityID){
            // print("preloadinggate");
            _this.entityID = entityID;
            var properties = Entities.getEntityProperties(_this.entityID, ['dimensions','position']);
            _this.closedYPosition = properties.position.y;
            var height = properties.dimensions.y;
            _this.openedYPosition = height + _this.closedYPosition;
        },
        raiseGate: function() {
            fullyClosed = false;
            // print("raising");
            var currentPosition = (Entities.getEntityProperties(_this.entityID, 'position').position).y;
            if (currentPosition.y < _this.openedYPosition) {
                currentPosition.y += MOVEMENT_INCREMENT;
                Entities.editEntity(_this.entityID, {
                    position: currentPosition
                });
            } else {
                fullyOpen = true;
            }
        },
        lowerGate: function() {
            fullyOpen = false;
            var currentPosition = Entities.getEntityProperties(_this.entityID, 'position').position;
            if (currentPosition.y > _this.closedYPosition) {
                currentPosition.y -= MOVEMENT_INCREMENT_LOWER;
                Entities.editEntity(_this.entityID, {
                    position: currentPosition
                });
            } else {
                fullyClosed = true;
            }
        },
        openGate: function() {
            fullyClosed = false;
            if (moving) {
                print("clearing closing interval");
                Script.clearInterval(moving);
            }
            print("opening gate again");
            print("open from " + (Entities.getEntityProperties(_this.entityID, 'position').position).y);
            print("open to " + _this.openedYPosition);
            moving = Script.setInterval(function() {
                // print("interval");
                if (!fullyOpen) {
                    _this.raiseGate();
                } else {
                    Script.clearInterval(moving);
                }
            }, MOVEMENT_INTERVAL_MS);
            print("gate is fully open");
        },
        closeGate: function() {
            fullyOpen = false;
            if (moving) {
                // print("clearing opening interval");
                Script.clearInterval(moving);
            }
            // print("closing gate");
            // print("closing from " + currentPosition.y);
            // print("closing to " + _this.closedYPosition);
            moving = Script.setInterval(function() {
                if (!fullyClosed) {
                    _this.lowerGate();
                } else {
                    Script.clearInterval(moving);
                }
            }, MOVEMENT_INTERVAL_LOWER_MS);
            // print("gate is fully closed");
        },
        stopMovement: function() {
            // print("stopping movement");
            if (moving) {
                // print("stopping movement");
                Script.clearInterval(moving);
            }
        },
        unload: function() {
            if (checking) {
                Script.clearInterval(checking);
            }
            if (moving) {
                Script.clearInterval(moving);
            }
        }
    };

    return new Gate();
});