import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { AccordionModule } from 'ngx-bootstrap/accordion';
import { ToastrService } from 'ngx-toastr';

import { UrlDisplayComponent } from './url-display.component';
import { AppConfigService } from '../../../../app-config.service';
import { FieldCriterion } from '../../../store/models/criterion';

describe('[Instance][Search][Component][Result] UrlDisplayComponent', () => {
    let component: UrlDisplayComponent;
    let fixture: ComponentFixture<UrlDisplayComponent>;
    let appConfigServiceStub = new AppConfigService();

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [UrlDisplayComponent],
            imports: [
                AccordionModule.forRoot(),
                BrowserAnimationsModule
            ],
            providers: [
                { provide: AppConfigService, useValue: appConfigServiceStub },
                { provide: ToastrService, useValue: { }}
            ]
        });
        fixture = TestBed.createComponent(UrlDisplayComponent);
        component = fixture.componentInstance;
    });

    it('should create the component', () => {
        expect(component).toBeTruthy();
    });

    it('#urlDisplayEnabled() should return if URL display has to be enabled or not', () => {
        component.datasetList = [
            {
                name: 'myDataset',
                table_ref: 'table',
                label: 'my dataset',
                description: 'This is my dataset',
                display: 1,
                data_path: '/path',
                survey_name: 'mySurvey',
                id_dataset_family: 1,
                public: true,
                full_data_path: '/data/path',
                config: {
                    images: ['image1'],
                    survey: {
                        survey_enabled: true,
                        survey_label: 'More about this survey'
                    },
                    cone_search: {
                        cone_search_enabled: true,
                        cone_search_opened: true,
                        cone_search_column_ra: 1,
                        cone_search_column_dec: 2,
                        cone_search_plot_enabled: true,
                        cone_search_sdss_enabled: true,
                        cone_search_sdss_display: 1,
                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
                    },
                    download: {
                        download_enabled: true,
                        download_opened: true,
                        download_csv: true,
                        download_ascii: true,
                        download_vo: true,
                        download_archive: true
                    },
                    summary: {
                        summary_enabled: true,
                        summary_opened: true
                    },
                    server_link: {
                        server_link_enabled: false,
                        server_link_opened: true
                    },
                    samp: {
                        samp_enabled: true,
                        samp_opened: true
                    },
                    datatable: {
                        datatable_enabled: true,
                        datatable_opened: true,
                        datatable_selectable_rows: true
                    }
                }
            },
            {
                name: 'anotherDataset',
                table_ref: 'table',
                label: 'another dataset',
                description: 'This is another dataset',
                display: 1,
                data_path: '/path',
                survey_name: 'mySurvey',
                id_dataset_family: 1,
                public: true,
                full_data_path: '/data/path',
                config: {
                    images: ['image1'],
                    survey: {
                        survey_enabled: true,
                        survey_label: 'More about this survey'
                    },
                    cone_search: {
                        cone_search_enabled: true,
                        cone_search_opened: true,
                        cone_search_column_ra: 1,
                        cone_search_column_dec: 2,
                        cone_search_plot_enabled: true,
                        cone_search_sdss_enabled: true,
                        cone_search_sdss_display: 1,
                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
                    },
                    download: {
                        download_enabled: true,
                        download_opened: true,
                        download_csv: true,
                        download_ascii: true,
                        download_vo: true,
                        download_archive: true
                    },
                    summary: {
                        summary_enabled: true,
                        summary_opened: true
                    },
                    server_link: {
                        server_link_enabled: true,
                        server_link_opened: true
                    },
                    samp: {
                        samp_enabled: true,
                        samp_opened: true
                    },
                    datatable: {
                        datatable_enabled: true,
                        datatable_opened: true,
                        datatable_selectable_rows: true
                    }
                }
            }
        ];
        component.datasetSelected = 'myDataset';
        expect(component.urlDisplayEnabled()).toBeFalsy();
        component.datasetSelected = 'anotherDataset';
        expect(component.urlDisplayEnabled()).toBeTruthy();
    });

    it('#urlDisplayOpened() should return if URL tab has to be opened or not', () => {
        component.datasetList = [
            {
                name: 'myDataset',
                table_ref: 'table',
                label: 'my dataset',
                description: 'This is my dataset',
                display: 1,
                data_path: '/path',
                survey_name: 'mySurvey',
                id_dataset_family: 1,
                public: true,
                full_data_path: '/data/path',
                config: {
                    images: ['image1'],
                    survey: {
                        survey_enabled: true,
                        survey_label: 'More about this survey'
                    },
                    cone_search: {
                        cone_search_enabled: true,
                        cone_search_opened: true,
                        cone_search_column_ra: 1,
                        cone_search_column_dec: 2,
                        cone_search_plot_enabled: true,
                        cone_search_sdss_enabled: true,
                        cone_search_sdss_display: 1,
                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
                    },
                    download: {
                        download_enabled: true,
                        download_opened: true,
                        download_csv: true,
                        download_ascii: true,
                        download_vo: true,
                        download_archive: true
                    },
                    summary: {
                        summary_enabled: true,
                        summary_opened: true
                    },
                    server_link: {
                        server_link_enabled: false,
                        server_link_opened: false
                    },
                    samp: {
                        samp_enabled: true,
                        samp_opened: true
                    },
                    datatable: {
                        datatable_enabled: true,
                        datatable_opened: true,
                        datatable_selectable_rows: true
                    }
                }
            },
            {
                name: 'anotherDataset',
                table_ref: 'table',
                label: 'another dataset',
                description: 'This is another dataset',
                display: 1,
                data_path: '/path',
                survey_name: 'mySurvey',
                id_dataset_family: 1,
                public: true,
                full_data_path: '/data/path',
                config: {
                    images: ['image1'],
                    survey: {
                        survey_enabled: true,
                        survey_label: 'More about this survey'
                    },
                    cone_search: {
                        cone_search_enabled: true,
                        cone_search_opened: true,
                        cone_search_column_ra: 1,
                        cone_search_column_dec: 2,
                        cone_search_plot_enabled: true,
                        cone_search_sdss_enabled: true,
                        cone_search_sdss_display: 1,
                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
                    },
                    download: {
                        download_enabled: true,
                        download_opened: true,
                        download_csv: true,
                        download_ascii: true,
                        download_vo: true,
                        download_archive: true
                    },
                    summary: {
                        summary_enabled: true,
                        summary_opened: true
                    },
                    server_link: {
                        server_link_enabled: true,
                        server_link_opened: true
                    },
                    samp: {
                        samp_enabled: true,
                        samp_opened: true
                    },
                    datatable: {
                        datatable_enabled: true,
                        datatable_opened: true,
                        datatable_selectable_rows: true
                    }
                }
            }
        ];
        component.datasetSelected = 'myDataset';
        expect(component.urlDisplayOpened()).toBeFalsy();
        component.datasetSelected = 'anotherDataset';
        expect(component.urlDisplayOpened()).toBeTruthy();
    });

    it('#getUrl() should construct url', () => {
        appConfigServiceStub.apiUrl = 'http://test.com';
        component.datasetSelected = 'dataset';
        component.outputList = [1, 2, 3];
        component.criteriaList = [];
        let url = component.getUrl();
        expect(url).toBe('http://test.com/search/dataset?a=1;2;3');
        component.criteriaList = [
            { id: 1, type: 'field', operator: 'eq', value: 'fd_crit_1' } as FieldCriterion,
            { id: 2, type: 'field', operator: 'eq', value: 'fd_crit_2' } as FieldCriterion
        ];
        url = component.getUrl();
        expect(url).toBe('http://test.com/search/dataset?a=1;2;3&c=1::eq::fd_crit_1;2::eq::fd_crit_2');
        component.criteriaList = [];
        component.coneSearch = { ra: 4, dec: 5, radius: 6 };
        url = component.getUrl();
        expect(url).toBe('http://test.com/search/dataset?a=1;2;3&cs=4:5:6');
    });
});