﻿function CropEditor(sequenceCommandExecutor, editPhotoService){
    this.cropOrderItemPanel = new CropOrderItemPanel(this);
    this.cropEditorEditPanel = new CropEditorEditPanel(this);    
    this.cropEditorProxy = new CropEditorProxy(this, editPhotoService);
    this.sequenceCommandExecutor = sequenceCommandExecutor;
}

CropEditor.prototype = {
    
    cropOrderItemPanel: null,
    cropEditorEditPanel: null,
    cropEditorProxy: null,
    sequenceCommandExecutor: null,
    cartItems: null,
    maxThumbPhotoSize: 90,
    maxPreviewPhotoSize: 320,
    selectedRpID: null,

    init:function(icartItems, rpID)
    {
        this.selectedRpID = rpID;
        this.clearUI();
        this.cartItems = icartItems; 
        this.populateCropAspectInfo(icartItems);
    },
    
    populateCropAspectInfo:function(cartItems){
        var eOrderItemIds = new Array();
        for (var i = 0; i < cartItems.SubItems.length; i++)
        {
            if ( ((cartItems.SubItems[i].Quantity > 0)) && (cartItems.SubItems[i].EncID != ""))
            {
                Array.add(eOrderItemIds, cartItems.SubItems[i].EncID);
            }
        }
        this.cropEditorProxy.getOrderItemCropWrappers(eOrderItemIds, this.maxThumbPhotoSize, this.maxPreviewPhotoSize);
    },
    
    populateCropAspectInfoComplete:function(orderItemCropWrappers){
        var j = 0;
        for (var i = 0; i < this.cartItems.SubItems.length; i++)
        {
            if ( ((this.cartItems.SubItems[i].OrigQuantity > 0) || (this.cartItems.SubItems[i].Quantity > 0)) && (this.cartItems.SubItems[i].EncID != "") && (orderItemCropWrappers[j] != null))
            {
                this.cropOrderItemPanel.addCropOrderItemBySubOrderItem(this.cartItems.SubItems[i], orderItemCropWrappers[j]);
                j++;
            }
        }
        this.render();
    },    
    
    ImagesRenderCompleted: function (){
        if (this.cropOrderItemPanel.ImagesRenderCompleted() && this.cropEditorEditPanel.ImagesRenderCompleted())
            return true;
        return false;
    },
    
    clearUI: function () {
        this.cropOrderItemPanel.clearUI();
        this.cropEditorEditPanel.clearUI();
    },
   
    clear: function() {
        this.selectedRpID = null;
        this.cartItems = null;
        this.cropOrderItemPanel.clear();
        this.cropEditorEditPanel.clear();
        this.cropEditorProxy.clear();
    },
        
    render:function()
    {                
        this.cropOrderItemPanel.render(this.selectedRpID);        
        var cropOrderItem = this.cropOrderItemPanel.getCurrentCropOrderItem();
        this.cropEditorEditPanel.render(cropOrderItem);             
    },
    
    renderCropOverlay: function()
    {
        this.cropOrderItemPanel.renderCropOverlay();
        this.cropEditorEditPanel.renderCropOverlay();
    },
    
    saveCropEdits: function()
    {
       this.cropEditorProxy.saveOrderItemCrops(this.cropOrderItemPanel.getOrderItemCropWrappers());
    },   
    
    closeCropEditor: function()
    {        
        this.clearUI();
        this.clear();        
    },
    
    newOrderItemSelected: function()
    {
        var cropOrderItem = this.cropOrderItemPanel.getCurrentCropOrderItem();        
        this.cropEditorEditPanel.updateImage(cropOrderItem);
    },
    
    showCropper:function()
    {
        this.cropEditorEditPanel.startCropper();
    },
    
    setCropOrientation: function(sRadioBtn, orientation)
    {
        this.cropEditorEditPanel.setCropOrientation(sRadioBtn, orientation);
        this.cropEditorEditPanel.startCropper();
    },
    
    startOverCrop: function()
    {
        this.cropEditorEditPanel.startOver();
        this.cropEditorEditPanel.startOver(); //TODO TEMP FIX:call twice or else not doing the cropper right. Have to fix later. 
        this.applyCrop(); //commit change immediately when clicking undo
    },
    
    applyCrop: function()
    {
        this.cropEditorEditPanel.saveEdit();
    },
    
    changeCrop: function()
    {
        this.cropEditorEditPanel.changeCrop();
    },
    getMinimumResOrderItemMedia: function (mediaID, rpid, currentAspectRatio) {
        this.cropEditorProxy.getMinimumResOrderItemMedia(mediaID, rpid, currentAspectRatio);
    },
    getMinimumResOrderItemMediaComplete: function(result)
    {
        this.cropEditorEditPanel.onCropGotMinSize(result);
    },
    saveOrderItemCropsComplete: function()
    {
        this.sequenceCommandExecutor.CommandCompleted();
    },
    getSavedCropToJSObject: function()
    {
        for(var i = 0; i < this.cartItems.SubItems.length; i++)
        {
            var rpID = this.cartItems.SubItems[i].rpID ? this.cartItems.SubItems[i].rpID : this.cartItems.SubItems[i].EncRetailerProductID;
            var foundCartItem = this.cropOrderItemPanel.getCartItemByRPId(rpID);  
            if (foundCartItem) this.cartItems.SubItems[i].WarnRes = foundCartItem.isLowRes;
        }
        return this.cartItems;
    }    
};
