/*
    This file is part of Accountant.

    Accountant is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with Accountant.  If not, see <http://www.gnu.org/licenses/>.
 */
// Account object
function account() {
    this.id=ko.observable();
    this.name=ko.observable();
    this.authorized_overdraft=ko.observable();
    this.future=ko.observable();
    this.current=ko.observable();
    this.pointed=ko.observable();
};

// The AccountController.
var AccountController = function($scope, $http, $rootScope) {
    // Account store and selection
    $scope.account = null;
    $scope.accounts = [];

    $scope.savedAccount = null;
    $scope.editingAccount = null;
    $scope.removedAccount = null;

    $scope.accountClass = function(account) {
        if(account == $scope.account) {
            return "active";
        }
    };

    $scope.valueClass = function(account, value) {
        if(value < account.authorized_overdraft) {
            return "text-error";
        } else if(value < 0) {
            return "text-warning";
        }
    };

    $scope.addAccount = function() {
      $scope.editingAccount = {
        id: null,
        name: null,
        authorized_overdraft: null
      };

      angular.element("#edit-account").modal();
    };

    $scope.show = function() {
      console.debug("show");
    };

    $scope.editAccount = function(account) {
      if(account) {
        $scope.editingAccount = account;
        $scope.savedAccount = angular.copy(account);
      }
    };

    $scope.cancelEditAccount = function(account, modalScope) {
      if($scope.savedAccount) {
        account.name = $scope.savedAccount.name;
        account.authorized_overdraft = $scope.savedAccount.authorized_overdraft;
      }

      $scope.editingAccount = null;
      $scope.savedAccount = null;

      if(modalScope && modalScope.dismiss) {
        modalScope.dismiss();
      }
    };

    $scope.saveAccount = function(account, modalScope) {
      // Ajax call to save the entry.
      var type;
      var url = "api/accounts";

      if(account.id) {
        url += "/" + account.id;
      }

      $http.put(url, angular.toJson(account)).success(function(data) {
        message("success", "Save", data);

        $scope.editingAccount = null;
        $scope.savedAccount = null;

        if(modalScope && modalScope.dismiss) {
          modalScope.dismiss();
        }

        // Reload accounts to update solds.
        $scope.loadAccounts();
      });
    };

    // Function to remove of an entry.
    $scope.accountRemove = function(account, modalScope) {
      $http.delete("api/accounts/" + account.id).success(function (data) {
        message("success", "Save", data);

        // Reload accounts to update solds.
        $scope.loadAccounts();

        if(modalScope && modalScope.dismiss) {
          modalScope.dismiss();
        }
      });
    };

    // Function to load accounts
    $scope.loadAccounts = function() {
      $http.get("api/accounts").success($scope.loadAccounts_success);
    };

    $scope.loadAccounts_success = function (data) {
        // Update accounts
        $scope.accounts = angular.fromJson(data);

        var accountToSelect = null

        // Reset selected account to the new instance corresponding to the old one.
        if($scope.account) {
            // Find the new instance of the previously selected account.
            angular.forEach($scope.accounts, function(account) {
                if(account.id == $scope.account.id) {
                    accountToSelect = account;
                }
            });
        }

        // Set selected account to first one if not yet selected
        if(!accountToSelect && $scope.accounts.length > 0){
            accountToSelect = $scope.accounts[0];
        }

        // Reset to account to select
        $scope.account = accountToSelect;

        $scope.$emit("accountsLoadedEvent", {account: $scope.account});
    };

    // Callback function to select a new account.
    $scope.selectAccount = function(account) {
      if(account) {
        $scope.account = account;

        $scope.$emit("accountsLoadedEvent", {account: $scope.account});
      }
    };

    $rootScope.$on("entrySavedEvent", $scope.loadAccounts);
    $rootScope.$on("entryRemovedEvent", $scope.loadAccounts);

    $scope.loadAccounts();
};

