import {WMSCapabilityConverter} from 'dasf-web/lib/map/model/ogcservice/WMSCapabilityConverter';
import { xml2js } from "xml-js";
import fs from 'fs'; 

describe("Read WMS Capabilities", ()=>{
    let cut: WMSCapabilityConverter;

    test('read wms version', async() => {
        var wmsCap: any = await readAWI_WMS130();
        var version: string = cut.getVersion();
        expect(version).toEqual("1.3.0");

        wmsCap = await readDE_WMS130();
        var version: string = cut.getVersion();
        expect(version).toEqual("1.3.0");

    });

    test('read provider name', async()=>{
        var wmsCap: any = await readAWI_WMS130();
        var name: string = cut.getProviderName();
        expect(name).toEqual("WMS");

        wmsCap = await readDE_WMS130();
        name = cut.getProviderName();
        expect(name).toEqual("WMS");
    });

    test('read provider title', async()=>{
        var wmsCap: any = await readAWI_WMS130();
        var result: string = cut.getProviderTitle();
        expect(result).toEqual("WMS");

        wmsCap = await readDE_WMS130();
        result = cut.getProviderTitle();
        expect(result).toEqual("");
    });

    test('read provider abstract', async()=>{
        var wmsCap: any = await readAWI_WMS130();
        var result: string = cut.getProviderAbstract();
        expect(result).toEqual("WMS");

        wmsCap = await readDE_WMS130();
        result = cut.getProviderAbstract();
        expect(result).toEqual("");
    });

    test('read available layers', async()=>{
        var wmsCap: any = await readAWI_WMS130();
        var result: { name: string; title: string; layerInfo: any, type: string }[] = cut.getLayers();
        expect(result.length).toEqual(34);

        result.forEach(layerData=>{
            expect(layerData.name).not.toBeUndefined();
            expect(layerData.title).not.toBeUndefined();
        });

        wmsCap = await readDE_WMS130();
        result = cut.getLayers();
        expect(result.length).toEqual(3);
        result.forEach(layerData=>{
            expect(layerData.name).not.toBeUndefined();
            expect(layerData.title).not.toBeUndefined();
        });
    });

    test('read layer legend url', async()=>{
        await readAWI_WMS130();
        var result: { name: string; title: string; layerInfo: any, type: string }[] = cut.getLayers();
        
        result.forEach(layerData=>{
            var url: string = cut.getLayerInfoLegenUrl(layerData.layerInfo);
            expect(url.length).toBeGreaterThanOrEqual(10);
        });
        
        await readDE_WMS130();
        result = cut.getLayers();
        
        result.forEach(layerData=>{
            var url: string = cut.getLayerInfoLegenUrl(layerData.layerInfo);
            expect(url.length).toBeGreaterThanOrEqual(10);
        });

    });


    async function readAWI_WMS130(): Promise<any>{
        var text = fs.readFileSync('./tests/assets/awiWMSGetCapabilities130.xml', 'utf-8');
        let result: any = xml2js(text, {compact: true});
        cut = new WMSCapabilityConverter(result);
        return result;
    }



    async function readDE_WMS130(): Promise<any>{
        var text = fs.readFileSync('./tests/assets/deWMSGetCapabilities130.xml', 'utf-8');
        let result: any = xml2js(text, {compact: true});
        cut = new WMSCapabilityConverter(result);
        return result;
    }

})