// @flow
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import sinon from 'sinon';
import { spies } from '@test';

import { PlotterParameterEditor as C } from '.';
import {  getValidPlotterparameterObj as getValidObj } from '@helpers/beat';

import testPlotterParameters from '@test/test_plotterparameters.json';
import testPlotters from '@test/test_plotters.json';

const libs = [];

describe('<PlotterParameterEditor />', () => {
	let wrapper;

	afterEach(() => {
		if(wrapper && wrapper.unmount)
			wrapper.unmount();
	});

	describe('accepts', () => {
		const plotparams = [
			{
				name: 'test/myplotterparameter/1',
				contents: {
					description: 'A basic plotterparameter as a sanity test for the PlotterParameterEditor',
					data: {
						legend_loc: 'string'
					},
					plotter: 'plot/bar/1'
				}
			}
		].concat(testPlotterParameters.map(a => getValidObj(a)));

		plotparams.forEach(function(plotparam){
			const saveFunc = () => {};
			const updateFunc = () => {};
			const plotters = testPlotters.filter(d => d.name.startsWith('plot')).map(testPlotters => testPlotters.name);
			it(`${ plotparam.name }`, () => {
				wrapper = mount(
					<C
						data={getValidObj(plotparam)}
						plotterparameters={plotparams}
						plotters={plotters}
						plotterNames={plotters}
						saveFunc={saveFunc}
						updateFunc={updateFunc}
					/>
				);
				expect(wrapper).to.have.props(
					['data', 'plotterparameters', 'plotters', 'saveFunc', 'updateFunc']
				);
			});
		});
	});

	describe('creates', () => {
		it('test/createplotparam/1', () => {
			const saveFunc = sinon.spy();
			const _updateFunc = (obj) => {
				wrapper.setProps && wrapper.setProps({ data: obj });
			};
			const updateFunc = sinon.spy(_updateFunc);
			const plotparamName = 'test/createplotparam/1';
			const plotters = testPlotters.filter(d => d.name.startsWith('plot')).map(testPlotters => testPlotters);
			wrapper = mount(
				<C
					data={getValidObj({name: plotparamName, contents: {}})}
					plotterparameters={[]}
					plotters={plotters}
					plotterNames={plotters}
					saveFunc={saveFunc}
					updateFunc={updateFunc}
				/>
			);

			expect(wrapper).to.have.props(
				['data', 'plotterparameters', 'plotters', 'saveFunc', 'updateFunc']
			);

			expect(wrapper.props().data).to.have.property('name', plotparamName);

			expect(wrapper.props().data.contents).to.have.property('description');
			expect(wrapper.props().data.contents).to.have.property('description').with.lengthOf(0);
			wrapper.find('#ppPl select').simulate('change', { target: { value: 'user/anotherploter/1'}});
			expect(updateFunc.callCount).to.equal(0);
			wrapper.find('#ppPl select').simulate('change', { target: { value: 'plot/bar/1'}});
			expect(updateFunc.callCount).to.equal(1);
			expect(wrapper.props().data.contents).to.have.property('plotter', 'plot/bar/1');

			expect(wrapper.props().data.contents).to.have.property('description').with.lengthOf(0);
			expect(wrapper.props().data.contents).to.have.property('plotter').with.lengthOf(10);
			expect(wrapper.props().data.contents).to.have.property('data');
			expect(wrapper.props().data.contents).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
				}
			});

			wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
			let savedObj = saveFunc.args[0][0].contents;
			expect(savedObj).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
				}
			});

			wrapper.find('ParameterConsume').at(0).find('input').simulate('change', { target: { value: '11'}});
			expect(wrapper.props().data.contents).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
					'axis-fontsize': 11,
				}
			});
			wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
			savedObj = saveFunc.args[1][0].contents;
			expect(savedObj).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
					'axis-fontsize': 11,
				}
			});

			wrapper.find('ParameterConsume').at(8).find('input').simulate('change', { target: { value: 'plot legend'}});
			expect(wrapper.props().data.contents).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
					'axis-fontsize': 11,
					'legend': 'plot legend',
				}
			});
			wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
			savedObj = saveFunc.args[2][0].contents;
			expect(savedObj).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
					'axis-fontsize': 11,
					'legend': 'plot legend',
				}
			});

			wrapper.find('ParameterConsume').at(0).find('input').simulate('change', { target: { value: '10'}});
			expect(wrapper.props().data.contents).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
					'legend': 'plot legend',
				}
			});
			wrapper.find('button.mx-auto.btn.btn-outline-secondary').simulate('click');
			savedObj = saveFunc.args[3][0].contents;
			expect(savedObj).to.deep.equal({
				'description': '',
				'plotter': 'plot/bar/1',
				'data': {
					'legend': 'plot legend',
				}
			});
		});
	});
});
