import { Tinymce } from '../../utils/tinymce';
import getConstant from '../../constants';
import 'bootstrap-3-typeahead';

$(() => {
  const grantIdField = $('.grant-id-typeahead');
  const grantIdHidden = $('input#plan_grant_number');

  Tinymce.init();
  $('#is_test').click((e) => {
    $('#plan_visibility').val($(e.target).is(':checked') ? 'is_test' : 'privately_visible');
  });

  const showHideDataContact = (el) => {
    if ((el).is(':checked')) {
      $('div.data-contact').fadeOut();
    } else {
      $('div.data-contact').fadeIn();
    }
  };

  $('#show_data_contact').click((e) => {
    showHideDataContact($(e.currentTarget));
  });
  showHideDataContact($('#show_data_contact'));

  // Toggle the disabled flags
  const toggleCheckboxes = (selections) => {
    $('#priority-guidance-orgs, #other-guidance-orgs').find('input[type="checkbox"]').each((i, el) => {
      const checkbox = $(el);
      if (selections.length >= getConstant('MAX_NUMBER_GUIDANCE_SELECTIONS')) {
        if (checkbox.is(':checked')) {
          checkbox.removeAttr('disabled');
        } else {
          checkbox.prop('disabled', true);
        }
      } else {
        checkbox.prop('disabled', false);
      }
    });
  };

  // Keep the modal window's guidance selections in line with selections on the main page
  const syncGuidance = (ctx) => {
    const currentList = $(ctx);
    const otherList = (currentList.attr('id') === 'priority-guidance-orgs' ? $('#other-guidance-orgs') : $('#priority-guidance-orgs'));
    const selections = currentList.find('input[type="checkbox"]:checked').map((i, el) => $(el).val()).get();
    otherList.find('input[type="checkbox"]').each((i, el) => {
      const checkbox = $(el);
      // Toggle the checked flag to match the current guidance list
      if (selections.indexOf(checkbox.val()) >= 0) {
        checkbox.prop('checked', true);
      } else {
        checkbox.prop('checked', false);
      }
    });
    toggleCheckboxes(selections);
  };

  const grantNumberInfo = (grantId) => {
    return `Grant number: ${grantId}`;
  }

  const setUpTypeahead = () => {
    if ($('.edit_plan').length) {
      $.get('/research_projects.json', (data) => {
        window.researchProjects = data;
        const descriptionData = $.map(data, datum => datum.description);
        grantIdField.typeahead({ source: descriptionData });
      }).then(function() {
        setInitialGrantProjectName();
      });
      grantIdField.on('change', () => {
        const current = grantIdField.typeahead('getActive');
        if (current) {
          // match or partial match found
          const currentResearchProject = window.researchProjects.find((datum) => {
            const fixString = string => String(string).toLowerCase();
            return fixString(datum.description) === fixString(current);
          });
          if (currentResearchProject) {
            const grantId = currentResearchProject.grant_id
            $('#grant_number_info').html(grantNumberInfo(grantId));
            grantIdHidden.val(grantId);
          }
        } else {
          $('#grant_number_info').html(grantNumberInfo(''));
          grantIdHidden.val('');
        }
      });
    }
  };

  const setInitialGrantProjectName = () => {
    const grantId = grantIdHidden.val();
    const researchProject = researchProjects.find(datum => datum.grant_id === grantId);
    if (researchProject) {
      grantIdField.val(researchProject.description);
    }
  };

  $('#other-guidance-orgs').find('input[type="checkbox"]').click((e) => {
    const checkbox = $(e.target);
    // Since this is the modal window, copy any selections over to the priority list
    if (checkbox.is(':checked')) {
      const priorityList = $('#priority-guidance-orgs');
      if (priorityList.find(`input[value="${checkbox.val()}"]`).length <= 0) {
        const li = checkbox.closest('li');
        // If its a subgroup copy the whole group otherwise just copy the line
        if (li.children('.sublist').length > 0) {
          priorityList.append(li.closest('ul').parent().clone());
        } else {
          priorityList.append(li.clone());
        }
      }
    }
    syncGuidance(checkbox.closest('ul[id]'));
  });

  $('#priority-guidance-orgs').find('input[type="checkbox"]').click((e) => {
    syncGuidance($(e.target).closest('ul[id]'));
  });

  toggleCheckboxes($('#priority-guidance-orgs input[type="checkbox"]:checked').map((i, el) => $(el).val()).get());

  setUpTypeahead();
});

@@ -2011,65 +2011,366 @@
 o = 
-(grantId) =%3E %7B%0A    return %60Grant number: $%7BgrantId%7D%60;%0A  %7D
+grantId =%3E %60Grant number: $%7BgrantId%7D%60;%0A%0A  const setInitialGrantProjectName = () =%3E %7B%0A    const grantId = grantIdHidden.val();%0A    const researchProjects = window.researchProjects;%0A    const researchProject = researchProjects.find(datum =%3E datum.grant_id === grantId);%0A    if (researchProject) %7B%0A      grantIdField.val(researchProject.description);%0A    %7D%0A  %7D;
 %0A%0A  
@@ -2677,28 +2677,15 @@
 hen(
-function() %7B%0A       
+() =%3E %7B
  set
@@ -2710,22 +2710,16 @@
 tName();
-%0A     
  %7D);%0A   
@@ -3219,16 +3219,17 @@
 grant_id
+;
 %0A       
@@ -3458,24 +3458,24 @@
 ;%0A        %7D%0A
+
       %7D);%0A  
@@ -3488,274 +3488,8 @@
 %7D;%0A%0A
-  const setInitialGrantProjectName = () =%3E %7B%0A    const grantId = grantIdHidden.val();%0A    const researchProject = researchProjects.find(datum =%3E datum.grant_id === grantId);%0A    if (researchProject) %7B%0A      grantIdField.val(researchProject.description);%0A    %7D%0A  %7D;%0A%0A
   $(

Fix JS lint errors