//
//  DocumentViewerController.js
//  CoverPage
//
//  Created by Richard Anaya on 1/9/2013.
//  Copyright (c) 2014 Evernote. All rights reserved.
//

var DocumentViewerController = function(pointer,resources,images,currentIndex) {
    this.pointer = pointer;
    this.resources = resources;
    this.images = images;
    this.currentIndex = currentIndex;
    this.exited = new Signal();
    this.lastCursorPos = {x:0, y:0};
    this.lastCursorTime = 0;
    this.hiddenFromLackOfMovement = false;
};

DocumentViewerController.prototype.wrap= function(min,max,val) {
    if(val<min){
        val = max + (val-min);
    }
    val %= max;
    return val;
}

DocumentViewerController.prototype.start = function() {
    Analytics.reportEvent('presentation_mode', 'navigation', 'view_image');
    
    this.imageViewer = new ImageViewerView(this.resources);
    this.imageViewer.showUI();
    this.imageViewer.setViewingImage(this.images[this.currentIndex].image);

    var _this = this;
    $encpJquery(document).on('keydown.documentmode',function(e){
        if(e.keyCode == 37){
            if(_this.currentIndex == 0){
                return;
            }
            _this.onPreviousDocumentRequested();
            e.preventDefault();
        }
        else if(e.keyCode == 39){
            if(_this.currentIndex == _this.images.length-1){
                return;
            }
            _this.onNextDocumentRequested();
            e.preventDefault();
        }
    });

    if(this.images.length >1){
        this.previousDocumentbutton = new PreviousDocumentButtonView(this.resources);
        this.previousDocumentbutton.showUI();
        this.previousDocumentbutton.setViewingImage(this.images[this.wrap(0,this.images.length,this.currentIndex-1)].image);
        this.previousDocumentbutton.clicked.add(this.onPreviousDocumentRequested,this);
        this.previousDocumentbutton.showCursorRequested.add(this.onShowCursorRequested,this);
        this.previousDocumentbutton.hideCursorRequested.add(this.onHideCursorRequested,this);

        this.nextDocumentbutton = new NextDocumentButtonView(this.resources);
        this.nextDocumentbutton.showUI();
        this.nextDocumentbutton.setViewingImage(this.images[this.wrap(0,this.images.length,this.currentIndex+1)].image);
        this.nextDocumentbutton.clicked.add(this.onNextDocumentRequested,this);
        this.nextDocumentbutton.showCursorRequested.add(this.onShowCursorRequested,this);
        this.nextDocumentbutton.hideCursorRequested.add(this.onHideCursorRequested,this);
    }

    this.updateNav();

    this.toolbar = new DocumentViewerToolbarView(this.resources);
    this.toolbar.showUI();
    this.toolbar.exitRequested.add(this.onExitRequested,this);
    this.toolbar.zoomOutRequested.add(this.onZoomOutRequested,this);
    this.toolbar.zoomInRequested.add(this.onZoomInRequested,this);
    this.toolbar.actualSizeRequested.add(this.onActualSizeRequested,this);
    this.toolbar.fillScreenRequested.add(this.onFillScreenRequested,this);
    this.toolbar.cursorChangeRequested.add(this.onCursorChangeRequested,this);
    this.toolbar.showCursorRequested.add(this.onShowCursorRequested,this);
    this.toolbar.hideCursorRequested.add(this.onHideCursorRequested,this);

    $encpJquery('html').css("overflow-y","hidden");

    //this.toolbar.displayFileName(this.images[this.currentIndex].filename);

    this.startPointerObserver();

    this.toolbar.onCursorChangeClick(this.pointer.currentCursor);
    $encpJquery('.documentViewerToolbarTooltip').css('right',"120px");
};

DocumentViewerController.prototype.updateNav = function(){
    if(this.images.length > 1 ){
        if(this.currentIndex == 0){
            this.previousDocumentbutton.hide();
        }
        else {
            this.previousDocumentbutton.show();
        }
        if(this.currentIndex == this.images.length-1){
            this.nextDocumentbutton.hide();
        }
        else {
            this.nextDocumentbutton.show();
        }
    }
};

DocumentViewerController.prototype.startPointerObserver = function() {
    if(!this.pointerObserverStarted){
        this.observerInterval = window.setInterval(_.bind(this.observePointer,this),200);
        this.pointerObserverStarted = true;
    }
};

DocumentViewerController.prototype.observePointer = function() {
    if(this.pointer && this.pointer.enabled){
        if(Math.sqrt(Math.pow(this.pointer.mouseX-this.lastCursorPos.x,2)+Math.pow(this.pointer.mouseY-this.lastCursorPos.y,2))<3){
            this.lastCursorTime += .2
        }
        else {
            this.lastCursorTime = 0;
            if(this.hiddenFromLackOfMovement) {
                this.pointer.show();
                this.hiddenFromLackOfMovement = false;
            }
        }
        this.lastCursorPos = {x:this.pointer.mouseX,y:this.pointer.mouseY};

        if(this.lastCursorTime > .5){
            if(!this.hiddenFromLackOfMovement){
                this.pointer.hide('none');
                this.hiddenFromLackOfMovement = true;
            }
        }
    }
}

DocumentViewerController.prototype.onShowCursorRequested = function(type){
    this.pointer.show(null,true);
};

DocumentViewerController.prototype.onHideCursorRequested = function(type){
    this.pointer.hide(null,true);
};

DocumentViewerController.prototype.onCursorChangeRequested = function(type){
    this.pointer.changeCursor(type);
};

DocumentViewerController.prototype.onZoomOutRequested = function() {
    Analytics.reportEvent('presentation_mode', 'tools', 'zoom_out');
    this.imageViewer.zoomOut();
};

DocumentViewerController.prototype.onZoomInRequested = function() {
    Analytics.reportEvent('presentation_mode', 'tools', 'zoom_in');
    this.imageViewer.zoomIn();
};

DocumentViewerController.prototype.onActualSizeRequested = function() {
    this.imageViewer.zoomToActualSize();
};

DocumentViewerController.prototype.onFillScreenRequested = function() {
    Analytics.reportEvent('presentation_mode', 'tools', 'fit');
    this.imageViewer.zoomToFillScreen();
};

DocumentViewerController.prototype.onExitRequested = function() {
    $encpJquery('.documentViewerToolbarTooltip').css('right',"170px");
    $encpJquery('html').css("overflow-y","auto");
    this.imageViewer.removeUI();
    if (this.previousDocumentbutton) this.previousDocumentbutton.removeUI();
    if (this.nextDocumentbutton) this.nextDocumentbutton.removeUI();
    this.toolbar.removeUI();
    this.exited.dispatch();;
};

DocumentViewerController.prototype.onPreviousDocumentRequested = function() {
    this.currentIndex = this.wrap(0,this.images.length,this.currentIndex-1);
    this.imageViewer.setViewingImage(this.images[this.currentIndex].image);
    this.previousDocumentbutton.setViewingImage(this.images[this.wrap(0,this.images.length,this.currentIndex-1)].image);
    this.nextDocumentbutton.setViewingImage(this.images[this.wrap(0,this.images.length,this.currentIndex+1)].image);
    this.updateNav();
    //this.toolbar.displayFileName(this.images[this.currentIndex].filename);
};

DocumentViewerController.prototype.onNextDocumentRequested = function() {
    this.currentIndex = this.wrap(0,this.images.length,this.currentIndex+1);
    this.imageViewer.setViewingImage(this.images[this.currentIndex].image);
    this.previousDocumentbutton.setViewingImage(this.images[this.wrap(0,this.images.length,this.currentIndex-1)].image);
    this.nextDocumentbutton.setViewingImage(this.images[this.wrap(0,this.images.length,this.currentIndex+1)].image);
    this.updateNav();
    //this.toolbar.displayFileName(this.images[this.currentIndex].filename);
};

DocumentViewerController.prototype.destroy = function() {
    $encpJquery(document).off('keydown.documentmode');
    window.clearInterval(this.observerInterval);
    if(this.imageViewer){ this.imageViewer.destroy(); }
    if(this.previousDocumentbutton){ this.previousDocumentbutton.destroy(); }
    if(this.nextDocumentbutton){ this.nextDocumentbutton.destroy(); }
    if(this.toolbar){  this.toolbar.destroy(); }
};