import inherits from 'inherits';

import PropertiesActivator from 'bpmn-js-properties-panel/lib/PropertiesActivator';

// Require all properties you need from existing providers.
// In this case all available bpmn relevant properties without camunda extensions.
import processProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/ProcessProps';
//import eventProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/EventProps';
//import linkProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/LinkProps';
import documentationProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/DocumentationProps';
import idProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/IdProps';
import nameProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/NameProps';
import {
  isAny
} from 'bpmn-js/lib/features/modeling/util/ModelingUtil';
import entryFactory from 'bpmn-js-properties-panel/lib/factory/EntryFactory';
//import spellProps from '../magic/parts/SpellProps';
//import axios from 'axios';
//require("layui-layer");
import Vue from 'vue';
// Require your custom property entries.
//import spellProps from './parts/SpellProps';
var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject;
var cmdHelper = require('bpmn-js-properties-panel/lib/helper/CmdHelper');

function ensureNotNull(prop) {
  if (!prop) {
    throw new Error(prop + ' must be set.');
  }

  return prop;
}

var setDefaultParameters = function (options) {

  // default method to fetch the current value of the input field
  var defaultGet = function (element) {
    var bo = getBusinessObject(element),
      res = {},
      prop = ensureNotNull(options.modelProperty);
    res[prop] = bo.get(prop);

    return res;
  };

  // default method to set a new value to the input field
  var defaultSet = function (element, values) {
    var res = {},
      prop = ensureNotNull(options.modelProperty);
    if (values[prop] !== '') {
      res[prop] = values[prop];
    } else {
      res[prop] = undefined;
    }

    return cmdHelper.updateProperties(element, res);
  };

  // default validation method
  var defaultValidate = function () {
    return {};
  };

  return {
    id: options.id,
    description: (options.description || ''),
    get: (options.get || defaultGet),
    set: (options.set || defaultSet),
    validate: (options.validate || defaultValidate),
    html: ''
  };
};


//配置右边状态栏
// The general tab contains all bpmn relevant properties.
// The properties are organized in groups.
function createGeneralTabGroups(element, bpmnFactory, elementRegistry, translate) {
  var generalGroup = {
    id: 'general',
    label: translate('General'),
    entries: []
  };

  if (isAny(element, ['bpmn:Process'])) {
    generalGroup.entries.push(entryFactory.textField({
      id: 'id',
      //description : 'Apply a black magic spell',
      label: '流程KEY',
      canBeDisabled: true,
      modelProperty: 'id'
    }));
  } else {
    idProps(generalGroup, element, translate);
  }

  nameProps(generalGroup, element, null, null, translate);
  processProps(generalGroup, element, translate);
  documentationProps(generalGroup, element, bpmnFactory, translate);
  var configureGroup = {
    id: 'keyDetails',
    label: 'keyDetails',
    entries: []
  }
  /*
  var detailsGroup = {
    id: 'Details',
    label: 'Details',
    entries: []
  };
  linkProps(detailsGroup, element, translate);
  eventProps(detailsGroup, element, bpmnFactory, elementRegistry, translate);
    var documentationGroup = {
      id: 'Documentation',
      label: '说明',
      entries: []
    };
  
    documentationProps(documentationGroup, element, bpmnFactory, translate);
  */
  return [
    generalGroup,
    configureGroup
    // detailsGroup
    // documentationGroup
  ]
}

// Create the custom magic tab
/*
function createMagicTabGroups(element, elementRegistry) {

  // Create a group called "Black Magic".
  var blackMagicGroup = {
    id: 'black-magic',
    label: 'Black Magic',
    entries: []
  };

  // Add the spell props to the black magic group.

  blackMagicGroup.entries.push(entryFactory.button({id:"id",label:"1111"}));
  spellProps(blackMagicGroup, element);
  return [
    blackMagicGroup
  ];
}*/

export default function MagicPropertiesProvider(
  eventBus, bpmnFactory, elementRegistry,
  translate) {

  PropertiesActivator.call(this, eventBus);

  this.getTabs = function (element) {

    var generalTab = {
      id: 'General',
      label: '基本信息',
      groups: createGeneralTabGroups(element, bpmnFactory, elementRegistry, translate)
    };

    // The "magic" tab
    /*
     var magicTab = {
       id: 'magic',
       label: 'Magic',
       groups: createMagicTabGroups(element, elementRegistry)
     };
  */
    // Show general + "magic" tab
    return [
      generalTab,
      //magicTab
    ];
  };
}
inherits(MagicPropertiesProvider, PropertiesActivator);


//创建自定义控件

entryFactory.button = function (options, defaultParameters) {
  var id = options.id,
    label = options.label || id,
    element = options.element;
  var $dialogVisible = Vue.prototype.store.state.dialogVisible;
  var _click = function processConfig() {
    //console.log(Vue.prototype.store);
    // console.log(element)
    if (element && element.constructor.name == "Root") {
      $dialogVisible.dialogIfromeSrc = $dialogVisible.url + "/ebpm-process-manage/procDefModelMgr/proConfig.do?procDefId=" + $dialogVisible.procDefId;
      //$dialogVisible.dialogIfromeSrc = $dialogVisible.url+'/ebpm-process-manage/procDefModelMgr/proConfig.do?procDefId=api:1:79097648-b4c9-11e9-8827-aa03cab5dae6';
      $dialogVisible.title = "流程设置";
    } else {
      if (defaultParameters.id == 'bdqx') {
        $dialogVisible.dialogIfromeSrc = $dialogVisible.url + "/plateform-web/#/roleFormField?formId=" + $dialogVisible.formId + "&procDefKey=" + $dialogVisible.procDefKey +
          "&appId=" + $dialogVisible.appId + "&actDefId=" + element.id + "&formCode=" + $dialogVisible.formCode + "&modelId=" + $dialogVisible.modelId
        $dialogVisible.title = element.businessObject.name + "环节表单设置";
      } else {
        $dialogVisible.dialogIfromeSrc = $dialogVisible.url + "/ebpm-process-manage/activityInfo/actConfig.do?" +
          "processDefId=" + $dialogVisible.procDefId + "&activityDefId=" + element.id + "&sys=" + $dialogVisible.appId;
        //$dialogVisible.dialogIfromeSrc = $dialogVisible.url+'/ebpm-process-manage/activityInfo/actConfig.do?' +
        //'procDefId=api:1:79097648-b4c9-11e9-8827-aa03cab5dae6&activityDefId=sid-20F588AB-471E-4E74-B2B1-FB46403E26DB';
        $dialogVisible.title = "环节设置";
      }
    }
    $dialogVisible.show = true;
    //console.log($dialogVisible);
  }
  var resource = defaultParameters;
  if (!$dialogVisible.procDefId) {
    resource.html = '<a style="display: block;" data-action="_click" class="el-button el-button--primary el-button--mini is-disabled">' + label + '</a>';
  } else {
    resource.html = '<a style="display: block;" data-action="_click" class="el-button el-button--primary el-button--mini">' + label + '</a>';
    resource['_click'] = _click;
  }
  return resource;
};