import {WFSConverter, WFSTag} from 'dasf-web/lib/map/model/ogcservice/WFSCapabilityConverter';
import fs from 'fs'; 
import { doesNotMatch } from 'node:assert';

/**
 * Test WFS Files
 * http://sfb806srv.uni-koeln.de/owsproxy.php?service=wfs&version=1.0.0&request=GetCapabilities
 */

describe("Read WFS Capabilities", ()=>{
    
    let cut: WFSConverter;

    test('read wfs version', async () =>{
        
        var doc = await readWFS200();
        var version: string = cut.getWFSVersion();
        expect(version).toEqual("2.0.0");

        doc = await readWFS110();
        var version: string = cut.getWFSVersion();
        expect(version).toEqual("1.1.0");

        doc = await koeln100();
        var version: string = cut.getWFSVersion();
        expect(version).toEqual("1.0.0");

        doc = await koeln110();
        var version: string = cut.getWFSVersion();
        expect(version).toEqual("1.1.0");

        doc = await koeln200();
        var version: string = cut.getWFSVersion();
        expect(version).toEqual("2.0.0");

        doc = await awi200();
        var version: string = cut.getWFSVersion();
        expect(version).toEqual("2.0.0");
    });

    test('read ProviderName', async () =>{
        var doc = await readWFS200();
        var result: string = cut.readTag(WFSTag.ProviderName);
        expect("Digital Earth @ GFZ").toEqual(result);

        doc = await readWFS110();
        result = cut.readTag(WFSTag.ProviderName);
        expect("Digital Earth @ GFZ").toEqual(result);

        doc = await koeln100();
        result = cut.readTag(WFSTag.ProviderName);
        expect("").toEqual(result);

        doc = await koeln110();
        result = cut.readTag(WFSTag.ProviderName);
        expect("").toEqual(result);

        doc = await koeln200();
        result = cut.readTag(WFSTag.ProviderName);
        expect("").toEqual(result);

        doc = await awi200();
        result = cut.readTag(WFSTag.ProviderName);
        expect("").toEqual(result);
    });

    test('read Title', async () =>{
        var doc = await readWFS200();
        var result: String = cut.readTag(WFSTag.Title);
        expect("").toEqual(result);

        doc = await readWFS110();
        result = cut.readTag(WFSTag.Title);
        expect("").toEqual(result);

        doc = await koeln100();
        result = cut.readTag(WFSTag.Title);
        expect("My GeoServer WFS").toEqual(result);

        doc = await koeln110();
        result = cut.readTag(WFSTag.Title);
        expect("My GeoServer WFS").toEqual(result);

        doc = await koeln200();
        result = cut.readTag(WFSTag.Title);
        expect("My GeoServer WFS").toEqual(result);

        doc = await awi200();
        result = cut.readTag(WFSTag.Title);
        expect("WFS").toEqual(result);
    });

    test('read FeatureTypes', async () =>{
        var doc = await readWFS200();
        var result: HTMLCollection = cut.readList(WFSTag.FeatureType);
        
        expect(result.length).toEqual(1);

        doc = await readWFS110();
        result = cut.readList(WFSTag.FeatureType);
        expect(1).toEqual(result.length);

        doc = await koeln100();
        result = cut.readList(WFSTag.FeatureType);
        expect(58).toEqual(result.length);

        doc = await koeln110();
        result = cut.readList(WFSTag.FeatureType);
        expect(58).toEqual(result.length);

        doc = await koeln200();
        result = cut.readList(WFSTag.FeatureType);
        expect(58).toEqual(result.length);

        doc = await awi200();
        result = cut.readList(WFSTag.FeatureType);
        expect(27).toEqual(result.length);
    });

    test('read ReadChildNodeTag', async () =>{
        await readWFS200();
        let featureList: HTMLCollection = cut.readList(WFSTag.FeatureType);
        
        for(let i:number=0;i<featureList.length;i++){
            let features: any = featureList[i];
            if(features){
                let layerName: string = cut.readChildNodeTag(features, WFSTag.Name);
                expect(layerName).toEqual("digitalearth:vgi_flood");
            }
        }

        await awi200();
        featureList = cut.readList(WFSTag.FeatureType);
        for(let i:number=0;i<featureList.length;i++){
            let features: any = featureList[i];
            
            if(features){
                let layerName: string = cut.readChildNodeTag(features, WFSTag.Name);
                console.log(layerName);
                expect(layerName.length).toBeGreaterThan(0);
            }
        }
        
    });

    test('read FloodMasks', async() => {
        await readFloodmasks();
        
        let featureList: HTMLCollection = cut.readList(WFSTag.FeatureType);
        for(let i:number=0;i<featureList.length;i++){
            let features: any = featureList[i];
            if(features){
                let layerName: string = cut.readChildNodeTag(features, WFSTag.Name);
                console.log(layerName);

                let abstract: string = cut.readChildNodeTag(features, WFSTag.Abstract);

                let boundingBox: Number[] = [];
                let lowerCornerString: string = cut.readChildNodeTag(features, "ows:LowerCorner");
                let upperCornerString: string = cut.readChildNodeTag(features, "ows:UpperCorner");
                let cornerString: string = lowerCornerString + " " + upperCornerString;
                console.log(cornerString.split(" "));
                cornerString.split(" ").forEach(coord => {
                    boundingBox.push(new Number(coord));                    
                });
                
            }
            break;
        }        
        
    })

    async function readWFS200(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/wfsGetCapabilities.xml', 'utf-8');
        var oParse = new DOMParser();
        var doc: XMLDocument = oParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

    async function readWFS110(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/dewfsGetCapabilities110.xml', 'utf-8');
        var oParse = new DOMParser();
        var doc: XMLDocument = oParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

    async function koeln100(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/koelnWFSGetCapabilities100.xml', 'utf-8');
        var oParse = new DOMParser();
        
        var doc: XMLDocument = oParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

    async function koeln110(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/koelnWFSGetCapabilities110.xml', 'utf-8');
        var oParse = new DOMParser();
        var doc: XMLDocument = oParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

    async function koeln200(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/koelnWFSGetCapabilities200.xml', 'utf-8');
        var oParse = new DOMParser();
        var doc: XMLDocument = oParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

    async function awi200(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/awiWFSGetCapabilities200.xml', 'utf-8');
        var oParse = new DOMParser();
        var doc: XMLDocument = oParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

    async function readFloodmasks(): Promise<XMLDocument>{
        var text = fs.readFileSync('./tests/assets/FloodMasksGetCapabilities.xml', 'utf-8');
        var domParse = new DOMParser();
        var doc: XMLDocument = domParse.parseFromString(text,'application/xml');
        cut = new WFSConverter(doc);
        return doc;
    }

});