// @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.only('<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': {
					'axis-fontsize': 10,
					'bar-alpha': 0.75,
					'bar-norm': true,
					'bar_attributes': '',
					'content_type': 'image/png',
					'dpi': 60,
					'grid': false,
					'height': 300,
					'legend': '',
					'legend-bbox-to-anchor': 1,
					'legend-fontsize': 12,
					'legend-loc': 'best',
					'title': 'Bar plot',
					'title-fontsize': 10,
					'width': 400,
					'xaxis_multiplier': 1,
					'xlabel': '',
					'yaxis_log': false,
					'yaxis_multiplier': 1,
					'ylabel': ''
				}
			});

		});
	});
});
