import { Subscription } from 'rxjs'

export class Node {
    isExpandable: boolean = true
    icon: string
    symbol: string
    store: string
    gisType: string = ''
    count: Number = 0
    tagPlugins: string[] = []
    // XXX: treeElement might be a list of TreeLayerItemComponent
    // XXX: circular dependency?
    treeElement: any //TreeLayerItemComponent

    constructor (
        public name: string,
        public title: string = '',
        public description: string = '',
        public children: Node[] = []
    ) {}

    hasChildren() {
        return true
    }
    isSelectable() {
        return true
    }
}

export class PrimaryGroupNode extends Node {
    isSelectable() {
        return false
    }

    downloadGpkg() {
        const layers = this.children.map(child => child.store).join(',')
        window.open(`/api/download/geodata/${layers}?format=gpkg`)
    }
}

export class LayerNode extends Node {
    isExpandable: boolean = false
    data: any
    hasPopups: boolean = false
    mapSubscription: Subscription

    constructor(
        public store: string,
        public name: string,
        public group: string,
        public icon: string,
        public symbol: string,
        public rawSurveyStore: string,
        public type: string,
        public gisType: string,
        public style: object,
        public zIndex: Number,
        public count: Number = 0,
        public description: string = '',
        public live: boolean = false,
        public custom: boolean = false,
        public tagPlugins: string[] = [],
        public viewableRole: string,
        public category?: string,
    ) {
        super(name)
    }

    getUrl() {
        return 'api/gj/'+this.store
    }

    hasChildren() {
        return false
    }

    downloadShapefile() {
        window.open(`/api/download/geodata/${this.store}?format=shapefile`)
    }

    downloadGpkg() {
        window.open(`/api/download/geodata/${this.store}?format=gpkg`)
    }

    downloadDXF(reproject: Boolean=false) {
        window.open(`/api/download/geodata/${this.store}?format=dxf&reproject=${reproject}`)
    }

    downloadRawData() {
        window.open('/download/rawdata/' + this.rawSurveyStore)
    }

    cancelDownload() {
        this.mapSubscription.unsubscribe()
    }
}

export class TreeData {
    constructor (
        public primaryGroups: PrimaryGroupNode[],
        public layerNodes: LayerNode[]
    ) {}
}