import React, { useContext } from 'react';
import LayerTree from '../../components/LayerTree/LayerTree';
import MapContext from '../../components/Map/MapContext';

/**
 * @component
 * @category LayerSwitcher
 */
function LayerSwitcherContainer (props){

    const { config, t } = props;

    const { map } = useContext(MapContext);

    const getLayerTreeModel = config => {
		let model = [];

		const baseLayerGroupId = config.base_layer_groups[0];
		for (let i=0; i<config.layer_groups.length; i++) {
			if (config.layer_groups[i].id != baseLayerGroupId) {
				let group = {
					title: config.layer_groups[i].title,
					key: 'group-' + config.layer_groups[i].id,
					checkable: false,
					selectable: false,
					isGroup: true,
					isLeaf: false,
					order: config.layer_groups[i].order,
					children: []
				}
	
				for (let k=0; k<config.layer_groups[i].layers.length; k++) {
					let layer = config.layer_groups[i].layers[k];
					let is_VPostgis = false;
					let source_type = 'wms';
					if (layer.type == 'v_PostGIS') {
						is_VPostgis = true;
						source_type = 'wms';
					}
					let layerConf = {
						title: layer.title,
						key: 'layer-' + layer.id, 
						isLeaf: false,
						icon: <></>,
						isLayer: true,
						wms_url: layer.wms_url,
						wfs_url: layer.wfs_url,
						type: source_type,
						ws: layer.workspace,
						name: layer.name,
						is_VPostgis: is_VPostgis,
						visible: layer.visible,
						writable: layer.writable,
						queryable: layer.queryable,
						latlong_extent: layer.latlong_extent,
						order: layer.order,
						selectable: false,
						symbol_stroke: 'rgba(19, 114, 216, 1.0)',
						symbol_fill: 'rgba(19, 114, 216, 0.3)',
						children: [{
							key: 'layer-' + layer.id + '-actions',
							parent: 'layer-' + layer.id,
							isLeaf: true,
							icon: <></>,
							checkable: false,
							selectable: false,
							showLayerActions: true
						}]
					};
					group.children.push(layerConf);
				}
				model.push(group);
			}
		}
		return model;
	};

	//let model = getLayerTreeModel(config);

    return (
        !map ? null : <LayerTree t={t} model={config.layerTree}/>
    );
}

export default LayerSwitcherContainer;