// Import modules.
var $ = require('jquery');
var _locationServices = require( './util/location-services' );
var _model = require( './worksheet-model' );
var _worksheet = require( './worksheet-controller' ); 
var config = require( './worksheet-config' ); 
var Handlebars = require("hbsfy/runtime");
require( '../secondary-nav' );
require('../nemo-shim');
require('jquery-easing');
require('cf-expandables');

// DOM references.
var _worksheetsDOM = document.querySelector('.page-contents');
var _btnNext = document.querySelector('.btn-worksheet-next');
var _btnPrev = document.querySelector('.btn-worksheet-prev');

// General app properties.
var _page = 1;
var TOTAL_PAGES = 3;
var _pageLoad = true;

function init() {
  // Load ?page=<page number> from URL or default to page 1.
  _page = Number(_locationServices.getURLParameter('page')) || 1;

  // Set default data in model if goals worksheet is not populated.  
  var worksheet = _model.getWorksheet('personal');
  if (!worksheet) {
    _model.setDefaultData();
  }
  _loadPage(_page);
}

function _nextClicked() {
  _page++;
  // Prevent double-clicks beyond bounds before button is disabled.
  if (_page > TOTAL_PAGES) _page = TOTAL_PAGES;
  _loadPage(_page);
}

function _prevClicked() {
  _page--;
  // Prevent double-clicks beyond bounds before button is disabled.
  if (_page < 1) _page = 1;
  _loadPage(_page);
}

function _updateNavigationState() {
  // At a middle page.
  if ( _page > 1 && _page < TOTAL_PAGES ) {
    _activateBtn( _btnNext, _nextClicked );
    _activateBtn( _btnPrev, _prevClicked );

  // At the first page.
  } else if ( _page === 1 ) {
    _activateBtn( _btnNext, _nextClicked );
    _deactivateBtn( _btnPrev, _prevClicked );

  // At the last page.
  } else {
    _deactivateBtn( _btnNext, _nextClicked );
    _activateBtn( _btnPrev, _prevClicked );
  }
}

function _activateBtn( btn, action ) {
  if ( btn.classList.contains( 'btn__disabled' ) ) {
    btn.addEventListener( 'mousedown', action, false );
    btn.classList.remove( 'btn__disabled' );
  }
}

function _deactivateBtn( btn, action ) {
  btn.removeEventListener( 'mousedown', action, false );
  btn.classList.add( 'btn__disabled' );
}

function _loadPage(page) {
  // Clear worksheet DOM.
  _worksheetsDOM.innerHTML = '';

  switch(Number(page)) {
  case 1 :
    _loadWorksheets();
    break;
  case 2 :
    _loadNotes();
    break;
  case 3 :
    _loadSummary();
    break;
  }
  _updateNavigationState();
  if (!_pageLoad) {
    $('html, body').animate({
      scrollTop: $(".page-contents").offset().top
    }, 500);
  }
  _pageLoad = false;
  $('.expandable').expandable();
}

/* // TEMP - DEBUG - display worksheet data in console.
document.querySelector('.official-website').addEventListener('mousedown', function() {
  console.log( 'git...', _model.getData().worksheets.goals );
} );
*/

function _loadWorksheets() {  
  // Generate page html 
  var pageTemplate = require( '../../templates/prepare-worksheets/page-worksheets.hbs' );
  var pageHtml = pageTemplate();
  _worksheetsDOM.innerHTML = pageHtml;

  // Add interactive sections for each of the worksheet types to the page
  var worksheetTypes = ['personal', 'financial', 'risks', 'flags'];
  for ( i = 0; i < worksheetTypes.length; i++ ) {
    var worksheet = _model.getWorksheet(worksheetTypes[i]);
    var rows = _model.filterEmptyRows(worksheet, {skipLast: true});

    var data = config.worksheetData[worksheetTypes[i]]();

    var options = {
      container: _worksheetsDOM.querySelector('.worksheet-' + worksheetTypes[i]),
      type: worksheetTypes[i],
      rows: rows,
      data: data
    }

    $.extend(options, config.worksheetModules[worksheetTypes[i]]());

    _worksheet.create(options); 
  };    
}

function _loadNotes() {
    var pageTemplate = require( '../../templates/prepare-worksheets/page-notes.hbs' );
    var pageHtml = pageTemplate();

    _worksheetsDOM.innerHTML = pageHtml;
    var goals = _model.filterEmptyRows(_model.combineGoals());
    var data = config.worksheetData['alternatives']();

    var options = $.extend({
    container: _worksheetsDOM.querySelector('.worksheet-notes'),
    type: 'notes',
    rows: goals,
    data: data
    }, config.worksheetModules['alternatives']());

    _worksheet.create(options);
}

function _loadSummary() {
    var pageTemplate = require( '../../templates/prepare-worksheets/page-summary.hbs' );
    var summarySection = require( '../../templates/prepare-worksheets/page-summary-section.hbs' );  
    var summaryError = require( '../../templates/prepare-worksheets/page-summary-error.hbs' );  
    Handlebars.registerPartial({
      'summarySection': summarySection,
      'summaryError': summaryError
    });

    var templateData = {summarySection: summarySection, summaryError: summaryError};
    var filterOpts = {requireGrade: true};
    var combinedGoals = _model.combineGoals();
    var filteredGoals = _model.filterEmptyRows(combinedGoals, filterOpts);
    var goals = templateData.goals = _model.sortWorksheetByGrade(filteredGoals, 'goals');
    // check for errors
    var goalErrors = config.errorMessages.goals;
    if (!combinedGoals.length) {
      templateData.goalsError = goalErrors.emptyInputs;
    } else if (!filteredGoals.length) {
      templateData.goalsError = goalErrors.noGrade;
    }
    
    var filteredRisks = _model.filterEmptyRows(_model.getWorksheet('risks'), filterOpts);
    var risks = templateData.risks = _model.sortWorksheetByGrade(filteredRisks, 'risks');
    // check for errors
    if (!filteredRisks.length) {
      var riskErrors = config.errorMessages.risks;
      templateData.risksError = riskErrors.noGrade;
    }
    
    var filteredFlags = _model.filterEmptyRows(_model.getWorksheet('flags'), filterOpts);
    var flags = templateData.flags = _model.sortWorksheetByGrade(filteredFlags, 'flags');
    // check for errors
    if (!filteredFlags.length) {
      var flagErrors = config.errorMessages.flags;
      templateData.flagsError = flagErrors.noGrade;
    }

    var pageHtml = pageTemplate(templateData);
    _worksheetsDOM.innerHTML = pageHtml;
    // routing hack, to load page 1 when error messages clicked
    $('.worksheet-summary a').click(function () {
        _loadPage(1);
    });
}

init();

@@ -2474,16 +2474,18 @@
 ) %7B%0A    
+//
 $('html,
@@ -2506,16 +2506,18 @@
 e(%7B%0A    
+//
   scroll
@@ -2562,12 +2562,14 @@
     
+//
 %7D, 
-5
+7
 00);
@@ -6099,16 +6099,22 @@
 ;%0A    //
+ HACK:
  routing
@@ -6190,16 +6190,40 @@
 ummary a
+:not(.expandable_target)
 ').click
@@ -6233,24 +6233,43 @@
 nction () %7B%0A
+        _page = 1;%0A
         _loa

fix hack to exempt expandables