var observable = require("data/observable");
var observableArray = require("data/observable-array");
var frame = require("ui/frame");
var view = require("ui/core/view");
var fileSystemService = require("~/data/fileSystemService");

var camera = require("camera");
var geolocation = require("nativescript-geolocation");

var scrapbook;
var page;
var updateStackLayout;

function scrapbookPageModel(){
    var model = new observable.Observable({ 
        title: null, 
        birthDate: null, 
        gender: null, 
        image: null,
        lat: null,
        long: null,
        isActive: false
    });

    model.calcAge = function(birthDate){
        var now = Date.now();
        var diff = Math.abs(now - birthDate) / 1000 / 31536000;

        return diff.toFixed(1);
    };
    
    return model;
}

exports.onLoaded = function(args) {
    page = args.object;
    updateStackLayout = view.getViewById(page, "updateStackLayout");
    
    var scrapbook = new observable.Observable({
        pages: new observableArray.ObservableArray(),
        selectedPage: null
    });

    var pages = fileSystemService.fileSystemService.getPages();

    if (pages.length !== 0) {
        pages.forEach(function (item) {
            var model = new scrapbookPageModel();
            
            model.id = item.id;
            model.title = item.title;
            model.gender = item.gender;
            model.birthDate = item.birthDate;
            model.image = item.image;
            model.lat = item.lat;
            model.long = item.long;
            model.isActive = item.isActive;
            
            scrapbook.pages.push(model);
        });
    }

    page.bindingContext = scrapbook;
};

exports.onAddTap = function(args) {
    var page = args.object;
    var scrapbook = page.bindingContext;
    
    scrapbook.pages.push(new scrapbookPageModel());
};

exports.onItemTap = function(args) {
    var scrapbook = page.bindingContext;

    scrapbook.pages.forEach(function (item) {
        item.set("isActive", false);
    });

    scrapbook.set("selectedPage", scrapbook.pages.getItem(args.index));
    updateStackLayout.bindingContext = scrapbook.selectedPage;
    
    scrapbook.selectedPage.set("isActive", true);
}

exports.onBirthDateTap = function(args) {
    var modalPageModule = "views/selectDate-page";
    var context = { birthDate: page.bindingContext.birthDate };
    var fullscreen = true;

    page.showModal(modalPageModule, context, function closeCallback(birthDate) {
        page.bindingContext.selectedPage.set("birthDate", birthDate);
    }, fullscreen);
};

exports.onGenderTap = function(args) {
    var modalPageModule = "views/selectGender-page";
    var context = { gender: page.bindingContext.gender };
    var fullscreen = true;
    page.showModal(modalPageModule, context, function closeCallback(gender) {
        page.bindingContext.selectedPage.set("gender", gender);
    }, fullscreen);
};

exports.onDoneTap = function(args) {    
    var scrapbook = page.bindingContext;
    
    fileSystemService.fileSystemService.savePage(scrapbook.selectedPage);

    scrapbook.selectedPage.set("isActive", false);
    scrapbook.set("selectedPage", null);
    updateStackLayout.bindingContext = scrapbook.selectedPage;
};

exports.onAddImageTap = function (args) {
    var scrapbookPage = updateStackLayout.bindingContext;

    if (!geolocation.isEnabled()) {
        geolocation.enableLocationRequest();
    }

    camera
        .takePicture({ width: 100, height: 100, keepAspectRatio: true })
        .then(function (picture) {
            scrapbookPage.set("image", picture);

            geolocation
                .getCurrentLocation()
                .then(function (location) {
                    scrapbookPage.set("lat", location.latitude);
                    scrapbookPage.set("long", location.longitude);
                });
        });
};
@@ -1735,36 +1735,8 @@
 ) %7B%0A
-    var page = args.object;%0A
     
@@ -1833,45 +1833,149 @@
 ));%0A
-%7D;%0A%0Aexports.onItemTap = function(args
+%0A    scrapbook.set(%22selectedPage%22, scrapbook.pages.getItem(scrapbook.pages.length - 1));%0A    resetActivePage();%0A%7D;%0A%0Afunction resetActivePage(
 ) %7B%0A
@@ -2011,24 +2011,28 @@
 ingContext;%0A
+    
 %0A    scrapbo
@@ -2106,24 +2106,160 @@
 se);%0A    %7D);
+%0A    %0A    scrapbook.selectedPage.set(%22isActive%22, true);%0A%7D%0A%0Aexports.onItemTap = function(args) %7B%0A    var scrapbook = page.bindingContext;
 %0A%0A    scrapb
@@ -2387,60 +2387,29 @@
 ge;%0A
-    
 %0A    
-scrapbook.selectedPage.set(%22isActive%22, true
+resetActivePage(
 );%0A%7D
@@ -2534,20 +2534,33 @@
 thDate: 
-page
+updateStackLayout
 .binding
@@ -2689,36 +2689,49 @@
 Date) %7B%0A        
-page
+updateStackLayout
 .bindingContext.
@@ -2726,37 +2726,24 @@
 dingContext.
-selectedPage.
 set(%22birthDa
@@ -2903,20 +2903,33 @@
 gender: 
-page
+updateStackLayout
 .binding
@@ -3055,28 +3055,41 @@
 ) %7B%0A        
-page
+updateStackLayout
 .bindingCont
@@ -3092,29 +3092,16 @@
 Context.
-selectedPage.
 set(%22gen

Refactor active page selection. Add auto-select new page.