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() {
        window.open('/download/layers/gpkg/' + this.children.map(child => child.store).join(','))
    }
}

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('/download/shapefile/' + this.store)
    }

    downloadGpkg() {
        window.open('/download/layers/gpkg/' + this.store)
    }

    downloadDXF(reproject: Boolean=false) {
        window.open('/download/layers/dxf/' + this.store + '?reproject=' + reproject)
    }

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

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

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