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

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

import { DownloadComponent } from './download.component';
import { AppConfigService } from '../../../../app-config.service';
import { FieldCriterion } from '../../../store/models/criterion';

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

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

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

    it('#isDownloadActivated() should return if download section 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: false,
                        summary_opened: false
                    },
                    server_link: {
                        server_link_enabled: false,
                        server_link_opened: true
                    },
                    samp: {
                        samp_enabled: false,
                        samp_opened: false
                    },
                    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: false,
                        download_opened: false,
                        download_csv: false,
                        download_ascii: false,
                        download_vo: false,
                        download_archive: false
                    },
                    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.isDownloadActivated()).toBeTruthy();
        component.datasetSelected = 'anotherDataset';
        expect(component.isDownloadActivated()).toBeFalsy();
    });

    it('#isDownloadOpened() should return if download 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: false,
                        summary_opened: false
                    },
                    server_link: {
                        server_link_enabled: false,
                        server_link_opened: false
                    },
                    samp: {
                        samp_enabled: false,
                        samp_opened: false
                    },
                    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: false,
                        download_opened: false,
                        download_csv: false,
                        download_ascii: false,
                        download_vo: false,
                        download_archive: false
                    },
                    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.isDownloadOpened()).toBeTruthy();
        component.datasetSelected = 'anotherDataset';
        expect(component.isDownloadOpened()).toBeFalsy();
    });

    it('#getConfigDownloadResultFormat() should return if download button for the given format has to be displayed', () => {
        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: false,
                        download_vo: true,
                        download_archive: true
                    },
                    summary: {
                        summary_enabled: false,
                        summary_opened: false
                    },
                    server_link: {
                        server_link_enabled: false,
                        server_link_opened: false
                    },
                    samp: {
                        samp_enabled: false,
                        samp_opened: false
                    },
                    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: false,
                        download_opened: false,
                        download_csv: false,
                        download_ascii: false,
                        download_vo: false,
                        download_archive: false
                    },
                    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.getConfigDownloadResultFormat('download_csv')).toBeTruthy();
        expect(component.getConfigDownloadResultFormat('download_ascii')).toBeFalsy();
    });

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

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

    it('#broadcastVotable() should raise broadcast event when clicked', () => {
        appConfigServiceStub.apiUrl = 'http://test.com';
        component.datasetSelected = 'myDataset';
        component.outputList = [1, 2, 3];
        component.criteriaList = [];
        component.broadcast.subscribe((event: string) => expect(event).toEqual('toto'));
        component.broadcastVotable();

    });
});