require('jquery');
require('jquery-mobile');
require('jquery.cookie');

// for flat-ui
require('jquery-ui');
require('jquery-ui-touch-punch');
require('../bower_components/bootstrap/dist/js/bootstrap.js');
require('bootstrap-switch');
require('bootstrap-tagsinput');
// require('holderjs');
// require('typeahead.js');
require('select2');
require('../bower_components/flat-ui/js/radiocheck.js');

import * as shiren4 from '../es6/model/shiren4.js';

$(() => {
  if (location.pathname == '/shiren4/') {
    $(':checkbox').checkboxradio();

    $('a.reset-checkboxes').click(() => {
      if(window.confirm('リセットしますか？'))
        clearCheckboxes();
    });

    loadFromCookie();
    $(':checkbox').change(() => saveToCookie());
  }
});

// Clear CheckBoxes "{{{1

var clearCheckboxes = () => {
  $(':checkbox')
  .prop('checked', false)
  .checkboxradio('refresh');

  saveToCookie();
};

// Save/Load Cookie Data "{{{1

var loadFromCookie = () => {
  if (typeof getCookie() === "undefined") return;

  var cookieData = JSON.parse(getCookie());
  Object.keys(cookieData)
  .forEach((key) => {
    $(`#${key} :checkbox`)
    .each((index, elem) =>
      $(elem)
      .prop('checked', cookieData[key][$(elem).data('id')])
      .checkboxradio('refresh')
    );
  });
};

var saveToCookie = () => {
  var ary = {};
    $(':checkbox')
    .each((index, val) => {
      var t = $(val).data('type');
      if (!(t in ary))
        ary[t] = {};
      ary[t][$(val).data('id')] = $(val).prop('checked');
    });
  var json = JSON.stringify(ary);
  setCookie(json);
};

var setCookie = (data) => {
  var thirtyDaysLater = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
  document.cookie = `data=${encodeURIComponent(data)};path=${location.pathname};expires=${thirtyDaysLater.toGMTString()}`;
}

var getCookie = () => {
	if (!document.cookie) return;

  var targetCookie =
    document.cookie
    .split("; ")
    .map((elem) => {
      var str = elem.split('=');
      return {key: str[0], value: str[1]};
    })
    .filter((cookie) => cookie.key == 'data');

  if (!targetCookie.some((cookie) => cookie.key == 'data')) return;

  return decodeURIComponent(targetCookie[0].value);
}

// __END__  "{{{1
// vim: expandtab softtabstop=2 shiftwidth=2
// vim: foldmethod=marker

require('jquery');
require('jquery-mobile');
require('jquery.cookie');

// for flat-ui
require('jquery-ui');
require('jquery-ui-touch-punch');
require('../bower_components/bootstrap/dist/js/bootstrap.js');
require('bootstrap-switch');
require('bootstrap-tagsinput');
// require('holderjs');
// require('typeahead.js');
require('select2');
require('../bower_components/flat-ui/js/radiocheck.js');

import * as shiren4 from '../es6/model/shiren4.js';

$(() => {
  if (location.pathname == '/shiren4/') {
    $(':checkbox').checkboxradio();

    $('a.reset-checkboxes').click(() => {
      if(window.confirm('リセットしますか？'))
        clearCheckboxes();
    });

    loadFromCookie();
    $(':checkbox').change(() => saveToCookie());
  }
});

// Clear CheckBoxes "{{{1

var clearCheckboxes = () => {
  $(':checkbox')
  .prop('checked', false)
  .checkboxradio('refresh');

  saveToCookie();
};

// Save/Load Cookie Data "{{{1

class CookieManager {
  constructor(doc) {
    this.doc = doc;
  }
}

var loadFromCookie = () => {
  if (typeof getCookie() === "undefined") return;

  var cookieData = JSON.parse(getCookie());
  Object.keys(cookieData)
  .forEach((key) => {
    $(`#${key} :checkbox`)
    .each((index, elem) =>
      $(elem)
      .prop('checked', cookieData[key][$(elem).data('id')])
      .checkboxradio('refresh')
    );
  });
};

var saveToCookie = () => {
  var ary = {};
    $(':checkbox')
    .each((index, val) => {
      var t = $(val).data('type');
      if (!(t in ary))
        ary[t] = {};
      ary[t][$(val).data('id')] = $(val).prop('checked');
    });
  var json = JSON.stringify(ary);
  setCookie(json);
};

var setCookie = (data) => {
  var thirtyDaysLater = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
  document.cookie = `data=${encodeURIComponent(data)};path=${location.pathname};expires=${thirtyDaysLater.toGMTString()}`;
}

var getCookie = () => {
	if (!document.cookie) return;

  var targetCookie =
    document.cookie
    .split("; ")
    .map((elem) => {
      var str = elem.split('=');
      return {key: str[0], value: str[1]};
    })
    .filter((cookie) => cookie.key == 'data');

  if (!targetCookie.some((cookie) => cookie.key == 'data')) return;

  return decodeURIComponent(targetCookie[0].value);
}

// __END__  "{{{1
// vim: expandtab softtabstop=2 shiftwidth=2
// vim: foldmethod=marker

Add CookieManager