import React from 'react';
import moment from 'moment';
import renderer from 'react-test-renderer';
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 c = v => console.log(`log >>>>`, v);

const defaultOptions = [
  { values: [moment().subtract(1, 'years')], operator: 'Gt', label: 'Trials (Last Year)' },
  { values: [moment()], operator: 'Gt', label: 'hoy'+moment() },
];

const initialValues = [
  { value: moment().subtract(2, 'days'), operator: 'Gt' },
  { value: [moment().subtract(2, 'days'), moment().subtract(1, 'days')], operator: 'Between' },
];

const mainSelectorOptions = {
  visible: true,
  selected: '',
  optionValues: {
    anyOption: { visible: true },
    beforeOption: { visible: true },
    betweenOption: { visible: true },
    afterOption: { visible: true },
    todayOption: { visible: true },
    onOption: { visible: true },
    customOptions: defaultOptions,
  },
};

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


describe('KdSelectDate', () => {
  let mainSelector;
  let kdFilterModel;
  beforeEach(() => {
    mainSelector = mainSelectorOptions;
    kdFilterModel = FilterItem('KdClinicalTrial', 'TrialListed', 'Date');
  });

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

    });
  });

  describe('ANY Option', () => {
    it('Visible', () => {
      mainSelector.optionValues.anyOption.visible = true;
      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.optionValues.anyOption.visible = 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', () => {
    it('Visible', () => {
      mainSelector.optionValues.afterOption.visible = true;
      const customProps = { ...props, mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find('option[value="Gt"]').length).toBe(1);
    });

    it('Select shows input', () => {
      mainSelector.optionValues.afterOption.visible = true;
      const customProps = { ...props, mainSelector, selectedValuesInitial: [] };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          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: 'AddChicklet', mainSelector };
      const wrapper = mount(
        <KdSelectDate
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      wrapper.find('.o-remove').simulate('click');
      expect(wrapper.node.getChicklets().length).toBe(initialValues.length - 1);
    });
  });
});
