import { criterionToString, stringToCriterion, getPrettyCriterion, getPrettyOperator } from './';
import {
    FieldCriterion,
    JsonCriterion,
    SelectMultipleCriterion,
    BetweenCriterion,
    Criterion,
    ListCriterion
} from '.';
import { Attribute } from '../../../metamodel/models';

describe('CriterionModel', () => {
    it('#criterionToString should convert criterion object to string', () => {
        let betweenCriterion: BetweenCriterion = { id: 1, type: 'between', min: 'un', max: null };
        let expected = '1::gte::un';
        expect(criterionToString(betweenCriterion)).toEqual(expected);
        betweenCriterion = { id: 1, type: 'between', min: null, max: 'deux' };
        expected = '1::lte::deux';
        expect(criterionToString(betweenCriterion)).toEqual(expected);
        betweenCriterion = { id: 1, type: 'between', min: 'un', max: 'deux' };
        expected = '1::bw::un|deux';
        expect(criterionToString(betweenCriterion)).toEqual(expected);
        const fieldCriterion = { id: 1, type: 'field', operator: 'eq', value: 'value' } as FieldCriterion;
        expected = '1::eq::value';
        expect(criterionToString(fieldCriterion)).toEqual(expected);
        const listCriterion = { id: 1, type: 'list', values: ['un', 'deux'] } as ListCriterion;
        expected = '1::in::un|deux';
        expect(criterionToString(listCriterion)).toEqual(expected);
        const jsonCriterion = { id: 1, type: 'json', path: 'path', operator: 'eq', value: 'value' } as JsonCriterion;
        expected = '1::js::path|eq|value';
        expect(criterionToString(jsonCriterion)).toEqual(expected);
        const selectMultipleCriterion = { id: 1, type: 'multiple', options: [{ label: 'un', value: '1', display: 1 }, { label: 'deux', value: '2', display: 2 }] } as SelectMultipleCriterion;
        expected = '1::in::1|2';
        expect(criterionToString(selectMultipleCriterion)).toEqual(expected);
    });

    it('#stringToCriterion should convert string criterion into object', () => {
        let attribute: Attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'field',
            operator: 'eq',
            type: 'field',
            display_detail: 1
        };
        let expected: Criterion = { id: 1, type: 'field', operator: 'neq', value: 'otherValue' } as FieldCriterion;
        expect(stringToCriterion(attribute, ['', 'neq', 'otherValue'])).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'field',
            operator: 'eq',
            type: 'field',
            min: 'value',
            display_detail: 1
        };
        expected = { id: 1, type: 'field', operator: 'eq', value: 'value' } as FieldCriterion;
        expect(stringToCriterion(attribute)).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'list',
            operator: 'eq',
            type: 'field',
            display_detail: 1
        };
        expected = { id: 1, type: 'list', values: ['one', 'two'] } as ListCriterion;
        expect(stringToCriterion(attribute, ['', '', 'one|two'])).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'list',
            operator: 'eq',
            type: 'field',
            min: 'valueA|valueB',
            display_detail: 1
        };
        expected = { id: 1, type: 'list', values: ['valueA', 'valueB'] } as ListCriterion;
        expect(stringToCriterion(attribute)).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'between',
            operator: 'eq',
            type: 'field',
            min: 'one',
            max: 'two',
            display_detail: 1
        };
        expected = { id: 1, type: 'between', min: 'valueA', max: 'valueB' } as BetweenCriterion;
        expect(stringToCriterion(attribute, ['', 'bw', 'valueA|valueB'])).toEqual(expected);
        expected = { id: 1, type: 'between', min: 'valueA', max: null } as BetweenCriterion;
        expect(stringToCriterion(attribute, ['', 'gte', 'valueA'])).toEqual(expected);
        expected = { id: 1, type: 'between', min: null, max: 'valueB' } as BetweenCriterion;
        expect(stringToCriterion(attribute, ['', 'lte', 'valueB'])).toEqual(expected);
        expected = { id: 1, type: 'between', min: 'one', max: 'two' } as BetweenCriterion;
        expect(stringToCriterion(attribute)).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'select-multiple',
            operator: 'eq',
            type: 'field',
            min: '1|2',
            display_detail: 1,
            options: [{ label: 'one', value: '1', display: 1 }, { label: 'two', value: '2', display: 2 }, { label: 'three', value: '3', display: 3 }]
        };
        expected = { id: 1, type: 'multiple', options: [{ label: 'one', value: '1', display: 1 }, { label: 'three', value: '3', display: 3 }] } as SelectMultipleCriterion;
        expect(stringToCriterion(attribute, ['', '', '1|3'])).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'select-multiple',
            operator: 'eq',
            type: 'field',
            min: '1|2',
            display_detail: 1,
            options: [{ label: 'one', value: '1', display: 1 }, { label: 'two', value: '2', display: 2 }, { label: 'three', value: '3', display: 3 }]
        };
        expected = { id: 1, type: 'multiple', options: [{ label: 'one', value: '1', display: 1 }, { label: 'two', value: '2', display: 2 }] } as SelectMultipleCriterion;
        expect(stringToCriterion(attribute)).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'json',
            operator: 'eq',
            type: 'field',
            display_detail: 1
        };
        expected = { id: 1, type: 'json', path: 'path', operator: 'op', value: 'value' } as JsonCriterion;
        expect(stringToCriterion(attribute, ['', '', 'path|op|value'])).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: 'json',
            operator: 'eq',
            type: 'field',
            min: 'path|op|value',
            display_detail: 1
        };
        expect(stringToCriterion(attribute)).toEqual(expected);
        attribute = {
            id: 1,
            name: 'myAttribute',
            label: 'my attribute',
            form_label: 'My Attribute',
            output_display: 1,
            criteria_display: 1,
            search_type: '',
            operator: 'eq',
            type: 'field',
            display_detail: 1
        };
        expect(stringToCriterion(attribute)).toBeNull();
    });

    it('#getPrettyCriterion should print criterion correctly', () => {
        const fieldCriterion = { id: 1, type: 'field', operator: 'eq', value: 'value' } as FieldCriterion;
        let expectedPrintedCriterion = '= value';
        expect(getPrettyCriterion(fieldCriterion)).toEqual(expectedPrintedCriterion);
        const jsonCriterion = { id: 1, type: 'json', path: 'path', operator: 'eq', value: 'value' } as JsonCriterion;
        expectedPrintedCriterion = 'path eq value';
        expect(getPrettyCriterion(jsonCriterion)).toEqual(expectedPrintedCriterion);
        const selectMultipleCriterion = { id: 1, type: 'multiple', options: [{label: 'un', value: '1', display: 1}, {label: 'deux', value: '2', display: 2}] } as SelectMultipleCriterion;
        expectedPrintedCriterion = '[un,deux]';
        expect(getPrettyCriterion(selectMultipleCriterion)).toEqual(expectedPrintedCriterion);
        let betweenCriterion = { id: 1, type: 'between', min: 'un', max: null } as BetweenCriterion;
        expectedPrintedCriterion = '>= un';
        expect(getPrettyCriterion(betweenCriterion)).toEqual(expectedPrintedCriterion);
        betweenCriterion = { id: 1, type: 'between', min: null, max: 'deux' } as BetweenCriterion;
        expectedPrintedCriterion = '<= deux';
        expect(getPrettyCriterion(betweenCriterion)).toEqual(expectedPrintedCriterion);
        betweenCriterion = { id: 1, type: 'between', min: 'un', max: 'deux' } as BetweenCriterion;
        expectedPrintedCriterion = '∈ [un;deux]';
        expect(getPrettyCriterion(betweenCriterion)).toEqual(expectedPrintedCriterion);
        const listCriterion = { id: 1, type: 'list', values: ['un', 'deux'] } as ListCriterion;
        expectedPrintedCriterion = '= [un,deux]';
        expect(getPrettyCriterion(listCriterion)).toEqual(expectedPrintedCriterion);
        const notCriterion = {id: 1, type: null} as Criterion;
        expectedPrintedCriterion = 'Criterion type not valid!';
        expect(getPrettyCriterion(notCriterion)).toEqual(expectedPrintedCriterion);
    });

    it('#getPrettyOperator() should prettify operator', () => {
        expect(getPrettyOperator('eq')).toEqual('=');
        expect(getPrettyOperator('neq')).toEqual('≠');
        expect(getPrettyOperator('gt')).toEqual('>');
        expect(getPrettyOperator('gte')).toEqual('>=');
        expect(getPrettyOperator('lt')).toEqual('<');
        expect(getPrettyOperator('lte')).toEqual('<=');
        expect(getPrettyOperator('lk')).toEqual('like');
        expect(getPrettyOperator('nlk')).toEqual('not like');
        expect(getPrettyOperator('in')).toEqual('in');
        expect(getPrettyOperator('nin')).toEqual('not in');
        expect(getPrettyOperator('toto')).toEqual('toto');
        expect(getPrettyOperator('')).toEqual('');
        expect(getPrettyOperator('')).toEqual('');
    });
});