import React from 'react';
import moment from 'moment';
import extend from 'extend';
import { mount } from 'enzyme';
import { FilterItem } from 'karmadata';

import KdSelectDate from '../../components/KdSelectDate';
import karmadataTheme from '../../themes/karmadata.scss';
import { chicklets, events, autoCompleteOptions } from '../__utils';

const opt = moment().subtract(1, 'years');

const mainSelectorOptions = {
  visible: true,
  notSelectedOption: { label: 'Any label', selected: true },
  optionGroups: [
    {
      label: 'group 1',
      options: [
        { values: [moment().add(1, 'days')], operator: 'Lt', label: 'Test Lt' },
        { values: [moment()], operator: 'Gt', label: 'Test Gt', selected: true, showTime: true },
        { values: [moment(), moment().subtract(1, 'month')], operator: 'Eq', label: 'Eq 2 chicklets' },
        { values: [moment(), moment().add(1, 'month')], operator: 'Between', label: 'Between from option' },
        { operator: 'Gte', label: 'Gte ' },
        { operator: 'Gt', label: 'Gt ' },
        { operator: 'Lt', label: 'Lt ' },
        { operator: 'Eq', label: 'Eq ' },
        { operator: 'Between', label: 'Between' },
      ],
    },
  ],
};

const props = {
  mainSelector: mainSelectorOptions,
  autoComplete: autoCompleteOptions,
  events,
  chicklets,
};

describe('KdSelectDate', () => {
  let mainSelector;
  let kdFilterModel;
  let initialValues;
  beforeEach(() => {
    mainSelector = extend({}, mainSelectorOptions);
    kdFilterModel = FilterItem('KdClinicalTrial', 'TrialListed', 'Date', 'Eq', [moment().subtract(2, 'days')]);
    initialValues = kdFilterModel.getSelected();
  });

  describe('Setup', () => {
    it('should set initialValues', () => {
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...props}
        />);
      expect(wrapper.node.getChicklets().length).toBe(initialValues.length);
      expect(kdFilterModel.getSelected().length).toBe(initialValues.length);
    });
  });

  describe('ANY Option', () => {
    it('Visible', () => {
      const customProps = { ...props, mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find(`option[value="any"]`).length).toBe(1);
    });

    it('On select clear values', () => {
      mainSelector.notSelectedOption = { label: 'Any label', selected: true };
      const customProps = { ...props, mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.node.getChicklets().length).toBe(initialValues.length);
      wrapper.find('select').simulate('change', { target: { value: 'any' } });
      expect(wrapper.node.getChicklets().length).toBe(0);
      expect(kdFilterModel.getSelected().length).toBe(0);
    });
  });

  describe('After Option - Gt/Gte', () => {
    it('Visible', () => {
      mainSelector.optionGroups = [{
        options: [{ operator: 'Gt', label: 'Gt ' }, { operator: 'Gte', label: 'Gte ' }],
      }];
      const customProps = { ...props, mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find('option[value="Gt"]').length).toBe(1);
      expect(wrapper.find('option[value="Gte"]').length).toBe(1);
    });

    it('Select shows input', () => {
      mainSelector.optionGroups = [{
        options: [{ operator: 'Gt', label: 'Gt ' }, { operator: 'Gte', label: 'Gte ' }],
      }];
      const customProps = { ...props, mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={FilterItem('KdClinicalTrial', 'TrialListed', 'Date')}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find('option[value="Gt"]').length).toBe(1);
      expect(wrapper.find('.c-kd__input').length).toBe(0);
      wrapper.find('select').simulate('change', { target: { value: 'Gt' } });
      expect(wrapper.find('.c-kd__input').length).toBe(1);
    });

    it('Remove Item', () => {
      const customProps = { ...props, selectBehavior: 'AddChickletSingleRegardlessOfType', mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      wrapper.find('.o-remove').first().simulate('click');
      expect(wrapper.node.getChicklets().length).toBe(initialValues.length - 1);
    });
  });

  describe('Behavior', () => {
    it('None', () => {
      const customProps = { ...props, selectBehavior: 'None', mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          {...customProps}
        />
      );

      expect(wrapper.find('.c-kd-selected-chicklets--chicklet_none')).not.toBeNull();
      expect(wrapper.find('.c-kd-selected-chicklets--chicklet_none < div').length).toBe(0);

      wrapper.find('select').simulate('change', { target: { value: 'Eq' } });
      const input = wrapper.find('.c-kd__input');

      input.simulate('change', { target: { value: '08/15/2017' } });
      expect(wrapper.find('.c-kd-selected-chicklets--chicklet_none')).not.toBeNull();
      expect(wrapper.find('.c-kd-selected-chicklets--chicklet_none < div').length).toBe(0);
    });

    it('AddChicklet', () => {
      const customProps = { ...props, selectBehavior: 'AddChicklet', mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          {...customProps}
        />
      );

      expect(wrapper.find('.c-kd-selected-chicklets')).not.toBeNull();
      wrapper.find('select').simulate('change', { target: { value: 'Eq' } });
      const input = wrapper.find('.c-kd__input');

      input.simulate('change', { target: { value: '08/15/2017' } });
      expect(wrapper.find('.c-kd-item-selected').length)
        .toBe(1);

      wrapper.find('select').simulate('change', { target: { value: 'Lt' } });
      input.simulate('change', { target: { value: '10/15/2017' } });
      expect(wrapper.find('.c-kd-item-selected').length)
        .toBe(1);
    });

    it('AddChickletSingle', () => {
      const customProps = { ...props, selectBehavior: 'AddChickletSingle', mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          {...customProps}
        />
      );

      expect(wrapper.find('.c-kd-selected-chicklets')).not.toBeNull();
      wrapper.find('select').simulate('change', { target: { value: 'Eq' } });
      const input = wrapper.find('.c-kd__input');

      input.simulate('change', { target: { value: '08/15/2017' } });
      expect(wrapper.find('.c-kd-item-selected').length)
        .toBe(1);

      wrapper.find('select').simulate('change', { target: { value: 'Lt' } });
      input.simulate('change', { target: { value: '10/15/2017' } });
      expect(wrapper.find('.c-kd-item-selected').length)
        .toBe(1);
    });
  });

  describe('validdation', () => {
    it('min', () => {
      const customProps = {
        ...props,
        mainSelector,
        selectBehavior: 'AddChicklet',
        min: moment().subtract(1, 'days'),
        selectedValuesInitial: [],
      };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={FilterItem('KdClinicalTrial', 'TrialListed', 'Date')}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'Eq' } });
      wrapper.find('.c-kd__input').simulate(
        'change',
        { target: { value: moment().subtract(5, 'days').format('MM/DD/YYYY') } },
      );

      expect(wrapper.node.getChicklets().length).toBe(0);
    });

    it('max', () => {
      const customProps = {
        ...props,
        mainSelector,
        selectBehavior: 'AddChicklet',
        max: moment(),
      };

      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={FilterItem('KdClinicalTrial', 'TrialListed', 'Date')}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'Eq' } });
      wrapper.find('.c-kd__input').simulate(
        'change',
        { target: { value: moment().add(5, 'days').format('MM/DD/YYYY') } },
      );

      expect(wrapper.node.getChicklets().length).toBe(0);
    });
  });

  describe('custom options', () => {
    it('visible', () => {
      const values = [moment(), moment().subtract(1, 'month')];
      mainSelector.optionGroups = [{
        options: [
          { values, operator: 'Eq', label: 'Eq 2 chicklets' },
        ],
      }];
      const customProps = {
        ...props,
        mainSelector,
        selectBehavior: 'AddChicklet',
      };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          {...customProps}
        />
      );
      const oValues = values.join('').replace(/\s/g, '-');
      expect(wrapper.find(`option[value="customEq${oValues}"]`).length).toBe(1);
    });
  });

  describe('events', () => {
    it('onChange', () => {
      const mockFunction = jest.fn();
      const customProps = {
        ...props,
        mainSelector,
        selectBehavior: 'AddChicklet',
        events: { onChange: mockFunction },
      };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={FilterItem('KdClinicalTrial', 'TrialListed', 'Date')}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'Lt' } });
      wrapper.find('.c-kd__input').simulate(
        'change',
        { target: { value: moment().format('MM/DD/YYYY') } },
      );
      expect(mockFunction.mock.calls.length).toBeGreaterThanOrEqual(1);
    });

    it('onSelect', () => {
      const mockFunction = jest.fn();
      mainSelector.optionGroups = [{
        options: [
          { operator: 'Eq', label: 'Eq 2 chicklets' },
        ],
      }];

      const customProps = {
        ...props,
        mainSelector,
        selectBehavior: 'AddChicklet',
        events: { onSelect: mockFunction },
      };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'Eq' } });
      expect(mockFunction.mock.calls.length).toBe(1);
    });
  });
});
