'use strict';
/**
 * Created by Martin on 24/02/2016.
 */

$.fn.pressEnter = function (fn) {

  return this.each(function () {
    $(this).bind('enterPress', fn);
    $(this).keyup(function (e) {
      if (e.keyCode === 13) {
        $(this).trigger('enterPress');
      }
    });
  });
};

var Keeper = (function () {

  var self = this;

  console.log('GO!');
  var $list = $('#listContainer');
  var displayList = function (obj) {
      var html = new EJS({url: '/partials/list.ejs'}).render(obj);
      console.log(html);
      $list.empty();
      $list.append(html);
      $('#listContainer').find('.entry').not('.emptyMessage').click(function () {
        console.log('Clicked list. ' + this.id);
        getRecipe(this.id);
      });
    }, displayPage = function (obj) {
      var $bodyContents = $('#bodyContents');

      if (obj.reduced.length > 0) {
        var vdata = {
          data: {
            _id: obj._id,
            _rev: obj._rev,
            title: obj.title,
            reduced: obj.reduced,
            url: obj.url
          }
        };

        var redoData = {
          _id: obj._id,
          _rev: obj._rev,
          url: obj.url
        };

        var html = new EJS({url: 'partials/view.ejs'}).render(vdata);

        $bodyContents.empty();
        $bodyContents.append(html);

        $('#redo').on('click', function () {
          self.trigger('redo', redoData);

        });

      }
    }, getRecipe = function (id) {
      console.log('get recipe');
      var url = '/entry/' + id;
      var data = '';
      $.ajax({
        type: 'GET',
        url: url,
        data: data,
        dataType: 'json',

        timeout: 10000,

        //contentType: ('application/json'),
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
          'Access-Control-Allow-Headers': 'Content-Type'

        },
        success: function (data) {
          //    console.log(data);
          displayPage(data);
        },
        error: function (xhr, type) {
          console.log('ajax error');
          console.log(xhr);
          console.log(type);
        }
      });
    },

    getList = function () {

      var url = '/list';

      $.ajax({
        type: 'GET',
        url: url,
        data: '',
        dataType: 'json',

        timeout: 10000,

        //contentType: ('application/json'),
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
          'Access-Control-Allow-Headers': 'Content-Type'

        },
        success: function (data) {
          // console.log(data);
          displayList(data);
        },
        error: function (xhr, type) {
          console.log('ajax error');
          console.log(xhr);
          console.log(type);
        }
      });
    }, addNew = function (newUrl) {
      var url = '/add';

      var data = {url: JSON.stringify(newUrl)};
      $.ajax({
        type: 'POST',
        url: url,
        data: data,
        dataType: 'json',

        timeout: 10000,

        //contentType: ('application/json'),
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
          'Access-Control-Allow-Headers': 'Content-Type'

        },
        success: function () {
          //  console.log(data);
          // displayList(data);
        },
        error: function (xhr, type) {
          console.log('ajax error');
          console.log(xhr);
          console.log(type);
        }
      });
    },
    redo = function (d) {
      console.log('redooing');
      var url = '/redo';

      $.ajax({
        type: 'POST',
        url: url,
        data: d,
        dataType: 'json',

        timeout: 10000,

        //contentType: ('application/json'),
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
          'Access-Control-Allow-Headers': 'Content-Type'

        },
        success: function () {
          //  console.log(data);
          // displayList(data);
        },
        error: function (xhr, type) {
          console.log('ajax error');
          console.log(xhr);
          console.log(type);
        }
      });
    },
    start = function () {

      getList();
    };

  $('#newurl').pressEnter(function () {
    var url = $(this).val();
    if (url !== null) {
      console.log('Adding: ' + url);
      addNew(url);
      $('#addstatus').fadeIn(400).delay(1500).fadeOut(400);
      $(this).val('');
      /* setTimeout(function () {
       getList();
       }, 5000);*/
    }
  });

  $('#fnRefresh').on('click', function () {
    getList();
  });

  this.bind('redo', function (data) {
    redo(data);
  });
  start();
});

MicroEvent.mixin(Keeper);

var keeper = new Keeper();
