'use strict';



//region Imports



var util = require('util');

var config = require('./shared/configuration-reader.js');

var Argument = require('./system/argument-check.js');

var Enumeration = require('./system/enumeration.js');



var ModelBase = require('./model-base.js');

var ModelError = require('./shared/model-error.js');

var ExtensionManager = require('./shared/extension-manager.js');

var EventHandlerList = require('./shared/event-handler-list.js');

var DataStore = require('./shared/data-store.js');

var DataType = require('./data-types/data-type.js');



var PropertyInfo = require('./shared/property-info.js');

var PropertyManager = require('./shared/property-manager.js');

var PropertyContext = require('./shared/property-context.js');

var ValidationContext = require('./rules/validation-context.js');

var TransferContext = require('./shared/transfer-context.js');



var RuleManager = require('./rules/rule-manager.js');

var DataTypeRule = require('./rules/data-type-rule.js');

var BrokenRuleList = require('./rules/broken-rule-list.js');

var RuleSeverity = require('./rules/rule-severity.js');

var AuthorizationAction = require('./rules/authorization-action.js');

var AuthorizationContext = require('./rules/authorization-context.js');

var BrokenRulesResponse = require('./rules/broken-rules-response.js');



var DataPortalAction = require('./shared/data-portal-action.js');

var DataPortalContext = require('./shared/data-portal-context.js');

var DataPortalEvent = require('./shared/data-portal-event.js');

var DataPortalEventArgs = require('./shared/data-portal-event-args.js');

var DataPortalError = require('./shared/data-portal-error.js');



var CLASS_NAME = 'ReadOnlyRootModel';

var MODEL_DESC = 'Read-only root model';

var M_FETCH = DataPortalAction.getName(DataPortalAction.fetch);



//endregion



/**

 * Factory method to create definitions of asynchronous read-only root models.

 *

 *    Valid child model types are:

 *

 *      * ReadOnlyChildCollection

 *      * ReadOnlyChildModel

 *

 * @function bo.ReadOnlyRootModel

 * @param {bo.shared.PropertyManager} properties - The property definitions.

 * @param {bo.shared.RuleManager} rules - The validation and authorization rules.

 * @param {bo.shared.ExtensionManager} extensions - The customization of the model.

 * @returns {ReadOnlyRootModel} The constructor of an asynchronous read-only root model.

 *

 * @throws {@link bo.system.ArgumentError Argument error}: The properties must be a PropertyManager object.

 * @throws {@link bo.system.ArgumentError Argument error}: The rules must be a RuleManager object.

 * @throws {@link bo.system.ArgumentError Argument error}: The extensions must be a ExtensionManager object.

 *

 * @throws {@link bo.shared.ModelError Model error}:

 *    The child objects must be ReadOnlyChildCollection or ReadOnlyChildModel instances.

 */

var ReadOnlyRootModelFactory = function (properties, rules, extensions) {

  var check = Argument.inConstructor(CLASS_NAME);



  properties = check(properties).forMandatory('properties').asType(PropertyManager);

  rules = check(rules).forMandatory('rules').asType(RuleManager);

  extensions = check(extensions).forMandatory('extensions').asType(ExtensionManager);



  // Verify the model type of child models.

  properties.verifyChildTypes([ 'ReadOnlyChildCollection', 'ReadOnlyChildModel' ]);



  // Get data access object.

  var dao = extensions.getDataAccessObject(properties.name);



  /**

   * @classdesc

   *    Represents the definition of an asynchronous read-only root model.

   * @description

   *    Creates a new asynchronous read-only root model instance.

   *

   *    _The name of the model type available as:

   *    __<instance>.constructor.modelType__, returns 'ReadOnlyRootModel'._

   *

   * @name ReadOnlyRootModel

   * @constructor

   * @param {bo.shared.EventHandlerList} [eventHandlers] - The event handlers of the instance.

   *

   * @extends ModelBase

   *

   * @throws {@link bo.system.ArgumentError Argument error}:

   *    The event handlers must be an EventHandlerList object or null.

   *

   * @fires ReadOnlyRootModel#preFetch

   * @fires ReadOnlyRootModel#postFetch

   */

  var ReadOnlyRootModel = function (eventHandlers) {

    ModelBase.call(this);



    eventHandlers = Argument.inConstructor(properties.name)

        .check(eventHandlers).forOptional('eventHandlers').asType(EventHandlerList);



    var self = this;

    var store = new DataStore();

    var brokenRules = new BrokenRuleList(properties.name);

    var isValidated = false;

    var propertyContext = null;

    var dataContext = null;



    // Set up business rules.

    rules.initialize(config.noAccessBehavior);



    // Set up event handlers.

    if (eventHandlers)

      eventHandlers.setup(self);



    //region Transfer object methods



    function getTransferContext (authorize) {

      return authorize ?

          new TransferContext(properties.toArray(), readPropertyValue, null) :

          new TransferContext(properties.toArray(), null, setPropertyValue);

    }



    function baseFromDto(dto) {

      properties.filter(function (property) {

        return property.isOnDto;

      }).forEach(function (property) {

        if (dto.hasOwnProperty(property.name) && typeof dto[property.name] !== 'function') {

          setPropertyValue(property, dto[property.name]);

        }

      });

    }



    function fromDto (dto) {

      if (extensions.fromDto)

        extensions.fromDto.call(self, getTransferContext(false), dto);

      else

        baseFromDto(dto);

    }



    function baseToCto() {

      var cto = {};

      properties.filter(function (property) {

        return property.isOnCto;

      }).forEach(function (property) {

        cto[property.name] = readPropertyValue(property);

      });

      return cto;

    }



    /**

     * Transforms the business object to a plain object to send to the client.

     *

     * @function ReadOnlyRootModel#toCto

     * @returns {object} The client transfer object.

     */

    this.toCto = function () {

      var cto = {};

      if (extensions.toCto)

        cto = extensions.toCto.call(self, getTransferContext(true));

      else

        cto = baseToCto();



      properties.children().forEach(function(property) {

        var child = getPropertyValue(property);

        cto[property.name] = child.toCto();

      });

      return cto;

    };



    //endregion



    //region Permissions



    function getAuthorizationContext(action, targetName) {

      return new AuthorizationContext(action, targetName || '', brokenRules);

    }



    function canBeRead (property) {

      return rules.hasPermission(

        getAuthorizationContext(AuthorizationAction.readProperty, property.name)

      );

    }



    function canDo (action) {

      return rules.hasPermission(

        getAuthorizationContext(action)

      );

    }



    function canExecute (methodName) {

      return rules.hasPermission(

        getAuthorizationContext(AuthorizationAction.executeMethod, methodName)

      );

    }



    //endregion



    //region Child methods



    function fetchChildren(dto, callback) {

      var count = 0;

      var error = null;



      function finish (err) {

        error = error || err;

        // Check if all children are done.

        if (++count === properties.childCount()) {

          callback(error);

        }

      }

      if (properties.childCount()) {

        properties.children().forEach(function(property) {

          var child = getPropertyValue(property);

          if (child instanceof ModelBase)

            child.fetch(dto[property.name], undefined, finish);

          else

            child.fetch(dto[property.name], finish);

        });

      } else

        callback(null);

    }



    function childrenAreValid() {

      return properties.children().every(function(property) {

        var child = getPropertyValue(property);

        return child.isValid();

      });

    }



    function checkChildRules() {

      properties.children().forEach(function(property) {

        var child = getPropertyValue(property);

        child.checkRules();

      });

    }



    function getChildBrokenRules (namespace, bro) {

      properties.children().forEach(function (property) {

        var child = getPropertyValue(property);

        var childBrokenRules = child.getBrokenRules(namespace);

        if (childBrokenRules) {

          if (childBrokenRules instanceof Array)

            bro.addChildren(property.name, childBrokenRules);

          else

            bro.addChild(property.name, childBrokenRules);

        }

      });

      return bro;

    }



    //endregion



    //region Data portal methods



    //region Helper



    function getDataContext (connection) {

      if (!dataContext)

        dataContext = new DataPortalContext(

          dao, properties.toArray(), getPropertyValue, setPropertyValue

        );

      return dataContext.setState(connection, false);

    }



    function raiseEvent (event, methodName, error) {

      self.emit(

          DataPortalEvent.getName(event),

          new DataPortalEventArgs(event, properties.name, null, methodName, error)

      );

    }



    function wrapError (error) {

      return new DataPortalError(MODEL_DESC, properties.name, DataPortalAction.fetch, error);

    }



    function runStatements (main, callback) {

      // Open connection.

      config.connectionManager.openConnection(

          extensions.dataSource, function (errOpen, connection) {

            if (errOpen)

              callback(wrapError(errOpen));

            else

              main(connection, function (err, result) {

                // Close connection.

                config.connectionManager.closeConnection(

                    extensions.dataSource, connection, function (errClose, connClosed) {

                      connection = connClosed;

                      if (err)

                        callback(wrapError(err));

                      else if (errClose)

                        callback(wrapError(errClose));

                      else

                        callback(null, result);

                    });

              });

          });

    }



    //endregion



    //region Fetch



    function data_fetch (filter, method, callback) {

      var hasConnection = false;

      // Helper function for post-fetch actions.

      function finish (dto, cb) {

        // Fetch children as well.

        fetchChildren(dto, function (err) {

          if (err)

            failed(err, cb);

          else {

            // Launch finish event.

            /**

             * The event arises after the business object instance has been retrieved from the repository.

             * @event ReadOnlyRootModel#postFetch

             * @param {bo.shared.DataPortalEventArgs} eventArgs - Data portal event arguments.

             * @param {ReadOnlyRootModel} newObject - The instance of the model after the data portal action.

             */

            raiseEvent(DataPortalEvent.postFetch, method);

            cb(null, self);

          }

        });

      }

      // Helper callback for failure.

      function failed (err, cb) {

        if (hasConnection) {

          // Launch finish event.

          var dpError = wrapError(err);

          raiseEvent(DataPortalEvent.postFetch, method, dpError);

        }

        cb(err);

      }

      // Main activity.

      function main (connection, cb) {

        hasConnection = connection !== null;

        // Launch start event.

        /**

         * The event arises before the business object instance will be retrieved from the repository.

         * @event ReadOnlyRootModel#preFetch

         * @param {bo.shared.DataPortalEventArgs} eventArgs - Data portal event arguments.

         * @param {ReadOnlyRootModel} oldObject - The instance of the model before the data portal action.

         */

        raiseEvent(DataPortalEvent.preFetch, method);

        // Execute fetch.

        if (extensions.dataFetch) {

          // *** Custom fetch.

          extensions.dataFetch.call(self, getDataContext(connection), filter, method, function (err, dto) {

            if (err)

              failed(err, cb);

            else

              finish(dto, cb);

          });

        } else {

          // *** Standard fetch.

          // Root element fetches data from repository.

          dao.$runMethod(method, connection, filter, function (err, dto) {

            if (err)

              failed(err, cb);

            else {

              fromDto.call(self, dto);

              finish(dto, cb);

            }

          });

        }

      }

      // Check permissions.

      if (method === M_FETCH ? canDo(AuthorizationAction.fetchObject) : canExecute(method))

        runStatements(main, callback);

      else

        callback(null, self);

    }



    //endregion



    //endregion



    //region Actions



    /**

     * Initializes a business object to be retrieved from the repository.

     * <br/>_This method is called by a factory method with the same name._

     *

     * @function ReadOnlyRootModel#fetch

     * @protected

     * @param {*} [filter] - The filter criteria.

     * @param {string} [method] - An alternative fetch method of the data access object.

     * @param {external.cbDataPortal} callback - Returns the required read-only business object.

     *

     * @throws {@link bo.system.ArgumentError Argument error}:

     *      The method must be a string or null.

     * @throws {@link bo.system.ArgumentError Argument error}:

     *      The callback must be a function.

     * @throws {@link bo.rules.AuthorizationError Authorization error}:

     *      The user has no permission to execute the action.

     * @throws {@link bo.shared.DataPortalError Data portal error}:

     *      Fetching the business object has failed.

     */

    this.fetch = function(filter, method, callback) {

      var check = Argument.inMethod(properties.name, 'fetch');



      method = check(method).forOptional('method').asString();

      callback = check(callback).forMandatory('callback').asFunction();



      data_fetch(filter, method || M_FETCH, callback);

    };



    //endregion



    //region Validation



    /**

     * Indicates whether all the validation rules of the business object, including

     * the ones of its child objects, succeeds. A valid business object may have

     * broken rules with severity of success, information and warning.

     *

     * _By default read-only business objects are supposed to be valid._

     *

     * @function ReadOnlyRootModel#isValid

     * @returns {boolean} True when the business object is valid, otherwise false.

     */

    this.isValid = function() {

      if (!isValidated)

        this.checkRules();



      return brokenRules.isValid() && childrenAreValid();

    };



    /**

     * Executes all the validation rules of the business object, including the ones

     * of its child objects.

     *

     * _By default read-only business objects are supposed to be valid._

     *

     * @function ReadOnlyRootModel#checkRules

     */

    this.checkRules = function() {

      brokenRules.clear();



      var context = new ValidationContext(store, brokenRules);

      properties.forEach(function(property) {

        rules.validate(property, context);

      });

      checkChildRules();



      isValidated = true;

    };



    /**

     * Gets the broken rules of the business object.

     *

     * _By default read-only business objects are supposed to be valid._

     *

     * @function ReadOnlyRootModel#getBrokenRules

     * @param {string} [namespace] - The namespace of the message keys when messages are localizable.

     * @returns {bo.rules.BrokenRulesOutput} The broken rules of the business object.

     */

    this.getBrokenRules = function(namespace) {

      var bro = brokenRules.output(namespace);

      bro = getChildBrokenRules(namespace, bro);

      return bro.$length ? bro : null;

    };



    /**

     * Gets the response to send to the client in case of broken rules.

     *

     * _By default read-only business objects are supposed to be valid._

     *

     * @function ReadOnlyRootModel#getResponse

     * @param {string} [message] - Human-readable description of the reason of the failure.

     * @param {string} [namespace] - The namespace of the message keys when messages are localizable.

     * @returns {bo.rules.BrokenRulesResponse} The broken rules response to send to the client.

     */

    this.getResponse = function (message, namespace) {

      var output = this.getBrokenRules(namespace);

      return output ? new BrokenRulesResponse(output, message) : null;

    };



    //endregion



    //region Properties



    function getPropertyValue(property) {

      return store.getValue(property);

    }



    function setPropertyValue(property, value) {

      store.setValue(property, value);

      //if (store.setValue(property, value))

      //  markAsChanged(true);

    }



    function readPropertyValue(property) {

      if (canBeRead(property)) {

        if (property.getter)

          return property.getter(getPropertyContext(property));

        else

          return store.getValue(property);

      } else

        return null;

    }



    function getPropertyContext(primaryProperty) {

      if (!propertyContext)

        propertyContext = new PropertyContext(properties.toArray(), readPropertyValue);

      return propertyContext.with(primaryProperty);

    }



    properties.map(function(property) {



      if (property.type instanceof DataType) {

        // Normal property

        store.initValue(property);



        Object.defineProperty(self, property.name, {

          get: function () {

            return readPropertyValue(property);

          },

          set: function (value) {

            throw new ModelError('readOnly', properties.name, property.name);

          },

          enumerable: true

        });



        rules.add(new DataTypeRule(property));



      } else {

        // Child item/collection

        if (property.type.create) // Item

          property.type.create(self, eventHandlers, function (err, item) {

            store.initValue(property, item);

          });

        else                      // Collection

          store.initValue(property, new property.type(self, eventHandlers));



        Object.defineProperty(self, property.name, {

          get: function () {

            return readPropertyValue(property);

          },

          set: function (value) {

            throw new ModelError('readOnly', properties.name, property.name);

          },

          enumerable: false

        });

      }

    });



    //endregion



    // Immutable object.

    Object.freeze(this);

  };

  util.inherits(ReadOnlyRootModel, ModelBase);



  /**

   * The name of the model type.

   *

   * @property {string} ReadOnlyRootModel.constructor.modelType

   * @default ReadOnlyRootModel

   * @readonly

   */

  Object.defineProperty(ReadOnlyRootModel, 'modelType', {

    get: function () { return CLASS_NAME; }

  });

  /**

   * The name of the model. However, it can be hidden by a model property with the same name.

   *

   * @name ReadOnlyRootModel#$modelName

   * @type {string}

   * @readonly

   */

  ReadOnlyRootModel.prototype.$modelName = properties.name;



  //region Factory methods



  /**

   * Retrieves a read-only business object from the repository.

   *

   * @function ReadOnlyRootModel.fetch

   * @param {*} [filter] - The filter criteria.

   * @param {string} [method] - An alternative fetch method of the data access object.

   * @param {bo.shared.EventHandlerList} [eventHandlers] - The event handlers of the instance.

   * @param {external.cbDataPortal} callback - Returns the required read-only business object.

   *

   * @throws {@link bo.system.ArgumentError Argument error}:

   *      The method must be a string or null.

   * @throws {@link bo.system.ArgumentError Argument error}:

   *      The event handlers must be an EventHandlerList object or null.

   * @throws {@link bo.system.ArgumentError Argument error}:

   *      The callback must be a function.

   * @throws {@link bo.rules.AuthorizationError Authorization error}:

   *      The user has no permission to execute the action.

   * @throws {@link bo.shared.DataPortalError Data portal error}:

   *      Fetching the business object has failed.

   */

  ReadOnlyRootModel.fetch = function(filter, method, eventHandlers, callback) {

    var instance = new ReadOnlyRootModel(eventHandlers);

    instance.fetch(filter, method, function (err) {

      if (err)

        callback(err);

      else

        callback(null, instance);

    });

  };



  //endregion



  return ReadOnlyRootModel;

};



module.exports = ReadOnlyRootModelFactory;