var myApp = angular.module("myApp", []);

myApp.controller("myAppCtrl", function($scope){
    $scope.mainApp = {
        mMainPageTitle: "Cost Tracker",
        mTotalSpendSoFar: 0.00,
        mTotalIncome: 0.00,
        myStyle: {color: 'red'},
        mIncludeFile: "mainpage.html",
        mMyExpenses: [],
        mMyIncome: [],
        mCurrentExpense: [],
        mViewExpID: 0,
        mViewExpDesc: "",
        LoadIncome: function(){
            $scope.mainApp.mMyIncome = []
            $scope.mainApp.mMyIncome = JSON.parse(window.localStorage.getItem("MyIncome"))
        },
        SaveNewIncome: function(){
            var tmpID = 1
            if($scope.mainApp.mMyIncome != null){
                if($scope.mainApp.mMyIncome.length == 0){
                    $scope.mainApp.mMyIncome = [{ID: tmpID, IncomeDesc: $scope.newIncome.mIncomeDesc, Amount: $scope.newIncome.mAmount, Date: $scope.newIncome.mDate, Time: $scope.newIncome.mTime, Place: $scope.newIncome.mPlace}]
                }
                else{
                    tmpID = $scope.mainApp.mMyIncome.slice(-1)[0].ID
                    tmpID ++
                    $scope.mainApp.mMyIncome.push({ID: tmpID, IncomeDesc: $scope.newIncome.mIncomeDesc,  Amount: $scope.newIncome.mAmount, Date: $scope.newIncome.mDate, Time: $scope.newIncome.mTime, Place: $scope.newIncome.mPlace})
                }
            }
            else{
                $scope.mainApp.mMyIncome = [{ID: 1, IncomeDesc: $scope.newIncome.mIncomeDesc,  
                                                 Amount: $scope.newIncome.mAmount, Date: $scope.newIncome.mDate, 
                                                 Time: $scope.newIncome.mTime, Place: $scope.newIncome.mPlace}]
            }
            window.localStorage.setItem("MyIncome",JSON.stringify($scope.mainApp.mMyIncome))     
              $scope.mainApp.GetTotalIncome()
              $scope.mainApp.backToMainPage()
        },
        LoadCostList: function(){
            $scope.mainApp.mMyExpenses = []
            $scope.mainApp.mMyExpenses = JSON.parse(window.localStorage.getItem("MyExpenses"))
        },
        SaveNewCost: function(){
            var tmpID = 1
            if($scope.mainApp.mMyExpenses != null){
                if($scope.mainApp.mMyExpenses.length == 0){
                    $scope.mainApp.mMyExpenses = [{ID: tmpID, CostDesc: $scope.newexpense.mCostDesc, Amount: $scope.newexpense.mAmount, Date: $scope.newexpense.mDate, Time: $scope.newexpense.mTime, Place: $scope.newexpense.mPlace}]
                }
                else{
                    tmpID = $scope.mainApp.mMyExpenses.slice(-1)[0].ID
                    tmpID ++
                    $scope.mainApp.mMyExpenses.push({ID: tmpID, CostDesc: $scope.newexpense.mCostDesc,  Amount: $scope.newexpense.mAmount, Date: $scope.newexpense.mDate, Time: $scope.newexpense.mTime, Place: $scope.newexpense.mPlace})
                }
            }
            else{
                $scope.mainApp.mMyExpenses = [{ID: 1, CostDesc: $scope.newexpense.mCostDesc,  
                                                 Amount: $scope.newexpense.mAmount, Date: $scope.newexpense.mDate, 
                                                 Time: $scope.newexpense.mTime, Place: $scope.newexpense.mPlace}]
            }
            window.localStorage.setItem("MyExpenses",JSON.stringify($scope.mainApp.mMyExpenses))     
              $scope.mainApp.GetTotalCost()
              $scope.mainApp.backToMainPage()
        },
        ResetCostList: function(){
            $scope.mainApp.mMyExpenses = []
            window.localStorage.setItem("MyExpenses",JSON.stringify($scope.mainApp.mMyExpenses))        
            $scope.mainApp.mMyIncome = []
            window.localStorage.setItem("MyIncome",JSON.stringify($scope.mainApp.mMyIncome))  
        },
        GetTotalCost: function(){
            if($scope.mainApp.mMyExpenses != null) 
                $scope.mainApp.mMyExpenses.forEach(function(val){$scope.mainApp.mTotalSpendSoFar += val.Amount})
            if($scope.mainApp.mTotalSpendSoFar != null && $scope.mainApp.mTotalIncome != null)
                if($scope.mainApp.mTotalSpendSoFar > $scope.mainApp.mTotalIncome)
                    $scope.mainApp.myStyle = {color: 'red'}
                else
                    $scope.mainApp.myStyle = {color: 'green'}
        },
        GetTotalIncome: function(){
            if($scope.mainApp.mMyIncome != null) 
                $scope.mainApp.mMyIncome.forEach(function(val){$scope.mainApp.mTotalIncome += val.Amount})
            if($scope.mainApp.mTotalSpendSoFar != null && $scope.mainApp.mTotalIncome != null)
                if($scope.mainApp.mTotalSpendSoFar > $scope.mainApp.mTotalIncome)
                    $scope.mainApp.myStyle = {color: 'red'}
                else
                    $scope.mainApp.myStyle = {color: 'green'}
        },
        ExpenseDetailPage: function(pExpID, pExpDesc){
            $scope.mainApp.mViewExpDesc = pExpDesc
            $scope.mainApp.mViewExpID = pExpID
            $scope.mainApp.mCurrentExpense = $scope.mainApp.mMyExpenses[$scope.mainApp.GetArrElement(pExpID)]
            //
            $scope.mainApp.mMainPageTitle = pExpDesc
            $scope.mainApp.mIncludeFile = "expensedetails.html"
            console.log($scope.mainApp.mCurrentExpense)
        }
        
    }
    
    $scope.mainApp.GetArrElement = function(pExpId){
        for(i=0; i <= $scope.mainApp.mMyExpenses.length - 1; i++)
        {
            if($scope.mainApp.mMyExpenses[i].ID == pExpId)
                return i;
        }
    };
    
    $scope.mainApp.LoadCostList();
    $scope.mainApp.LoadIncome();
    $scope.mainApp.GetTotalCost();
    $scope.mainApp.GetTotalIncome();
    
    $scope.mainApp.getCOstListFromMem = function(){
        JSON.parse(window.localStorage.getItem("MyExpenses"))
    };

    
    $scope.mainApp.loadNewCostForm = function(){
        $scope.mainApp.mMainPageTitle = "New Cost";
        $scope.mainApp.mIncludeFile = "newexpense.html";
    };
    
    $scope.mainApp.loadNewIncomeForm = function(){
        $scope.mainApp.mMainPageTitle = "New Income";
        $scope.mainApp.mIncludeFile = "newincome.html";
    };
    
    $scope.mainApp.backToMainPage = function(){
        $scope.mainApp.mMainPageTitle = "Cost Tracker";
        $scope.mainApp.mIncludeFile = "mainpage.html";
        $scope.newexpense.mAmount = "";
        $scope.newexpense.mCostDesc = "";
        $scope.newexpense.mDate = "";
        $scope.newexpense.mTime = "";
        $scope.newexpense.mPlace = "";
        $scope.newexpense.mId = "";
    };
    
    $scope.mainApp.loadCostListPage = function(){
        $scope.mainApp.mMainPageTitle = "My Costs";
        $scope.mainApp.mIncludeFile = "expenselist.html";
    };
    
    $scope.mainApp.loadIncomeListPage = function(){
        $scope.mainApp.mMainPageTitle = "My Income";
        $scope.mainApp.mIncludeFile = "incomelist.html";
    };
    
    $scope.newexpense = {
        mCostDesc: "",
        mAmount: "",
        mDate: new Date(),
        mTime: 10.00,
        mPlace: "",
        mId: 1,
        mPlace: ""
    }
    
    $scope.newIncome = {
        mIncomeDesc: "",
        mAmount: "",
        mDate: new Date(),
        mTime: 10.00,
        mId: 1,
    }
    
    $scope.CurrentExp = {
        mCostDesc: "",
        mAmount: "",
        mDate: new Date(),
        mTime: 10.00,
        mPlace: "",
        mId: 1,
        mPlace: ""
    }

});