//当前页面业务
"use script"; //开发环境建议开启严格模式

//对应widget.js中MyWidget实例化后的对象
var thisWidget;
var layersObj = {};

function initWidgetView(_thisWidget) {
  thisWidget = _thisWidget;

  //初始化树
  var setting = {
    check: {
      enable: true,
    },
    data: {
      simpleData: {
        enable: true,
      },
    },
    callback: {
      onCheck: treeOverlays_onCheck,
      onDblClick: treeOverlays_onDblClick,
    },
    view: {
      // addDiyDom: addOpacityRangeDom
    },
  };

  var zNodes = [];
  var layers = thisWidget.getLayers();
  for (var i = layers.length - 1; i >= 0; i--) {
    var node = _getNodeConfig(layers[i]);
    if (node) {
      zNodes.push(node);
    }
  }
  $.fn.zTree.init($("#treeOverlays_left"), setting, zNodes);
}

function _getNodeConfig(layer) {
  if (layer == null || !layer.options || layer.options.noLayerManage) {
    return;
  }

  var item = layer.options;

  if (!item.name) {
    console.log("未命名图层不加入图层管理", layer);
    return;
  }

  var node = {
    id: layer.id,
    pId: layer.pid,
    name: layer.name,
    uuid: layer.uuid,
    checked: layer.isAdded && layer.show,
  };

  if (layer.hasEmptyGroup) {
    //空数组
    node.icon = "../manageLayers/img/folder.png";
    node.open = item.open == null ? true : item.open;
  } else if (layer.hasChildLayer) {
    //有子节点的数组
    node.icon = "../manageLayers/img/layerGroup.png";
    node.open = item.open == null ? true : item.open;
  } else {
    node.icon = "../manageLayers/img/layer.png";
    if (layer.parent) {
      node._parentId = layer.parent.uuid;
    }
  }
  //记录图层
  layersObj[node.uuid] = layer;
  return node;
}

function treeOverlays_onDblClick(event, treeId, treeNode) {
  if (treeNode == null || treeNode.uuid == null) {
    return;
  }
  var layer = layersObj[treeNode.uuid];
  if (layer == null) {
    return;
  }
  layer.flyTo();
}

function treeOverlays_onCheck(e, treeId, chktreeNode) {
  var treeObj = $.fn.zTree.getZTreeObj(treeId);
  //获得所有改变check状态的节点
  var changedNodes = treeObj.getChangeCheckedNodes();
  for (var i = changedNodes.length - 1; i >= 0; i--) {
    var treeNode = changedNodes[i];
    treeNode.checkedOld = treeNode.checked;

    if (treeNode.check_Child_State == 1) {
      // 0:无子节点被勾选,  1:部分子节点被勾选,  2:全部子节点被勾选, -1:不存在子节点 或 子节点全部设置为 nocheck = true
      continue;
    }
    var layer = layersObj[treeNode.uuid];
    if (layer == null) {
      continue;
    }

    //显示隐藏透明度设置view
    // if (treeNode.checked)
    //     $("#" + treeNode.tId + "_range").show();
    // else
    //     $("#" + treeNode.tId + "_range").hide();

    //特殊处理同目录下的单选的互斥的节点，可在config对应图层节点中配置"radio":true即可
    if (layer.options.radio && treeNode.checked) {
      var nodes = treeObj.getNodesByFilter(
        function (node) {
          var item = layersObj[node.uuid];
          return item.options.radio && item.pid == layer.pid && node.uuid != treeNode.uuid;
        },
        false,
        treeNode.getParentNode()
      );
      for (var nidx = 0; nidx < nodes.length; nidx++) {
        nodes[nidx].checkedOld = false;
        treeObj.checkNode(nodes[nidx], false, true);

        $("#" + nodes[nidx].tId + "_range").hide();

        var layertmp = layersObj[nodes[nidx].uuid];
        layertmp.show = false;
      }
    }

    //处理图层显示隐藏
    thisWidget.updateLayerShow(layer, treeNode.checked);
  }
}
