    ﻿import { Zondy } from '../../../base'
    import BaseServer from '../../BaseServer'
    import { newGuid, extendDeep, defaultValue, isNull, Log } from '../../../util'
    import CMultiClassTheme from './CMultiClassTheme'
    import CSimpleTheme from './CSimpleTheme'
    import CChartTheme from './CChartTheme'
    import CGraduatedSymbolTheme from './CGraduatedSymbolTheme'
    import CDotDensityTheme from './CDotDensityTheme'
    import CRandomTheme from './CRandomTheme'
    import CFourColorTheme from './CFourColorTheme'
    import CUniqueTheme from './CUniqueTheme'
    import CRangeTheme from './CRangeTheme'
    import FolderInfoAttribute from './FolderInfoAttribute'
    import FolderInfo from './FolderInfo'
    import ThemesInfo from './ThemesInfo'

    /**
     * 专题图服务
     * @class ThemeServer
     * @moduleEx ServiceModule
     * @extends BaseServer
     * @param {Object} options 构造参数
     * @param {String} [options.url] 服务基地址
     * @example
     * // ES5引入方式
     * const { ThemeServer } = Zondy.Service
     * // ES6引入方式
     * import { ThemeServer } from "@mapgis/webclient-common"
     * const themeServer = new ThemeServer({
     *   url: 'http://192.168.82.89:6163/igs/rest/theme/Hubei4326'
     * });
     */
    class ThemeServer extends BaseServer {
      constructor(options) {
        options = defaultValue(options, {})
        super(options)
        /**
         * @private
         * @type {String}
         * @description 客户端标识，用以服务器缓存地图
         * @default ""
         */
        this.guid = defaultValue(options.guid, newGuid())
      }

      /** *
       * @description 解析专题图信息
       * @param {Object} jsonObj 专题图信息Json对象
       */
      formatThemInfo(jsonObj) {
        const folderInfo = new FolderInfo()
        extendDeep(folderInfo, jsonObj.result || jsonObj)
        if (
          folderInfo !== null &&
          folderInfo.attribute !== null &&
          folderInfo.attribute.length > 0
        ) {
          const themeInfoArray = [] // new ThemesInfo[folderInfo.attribute.Length];
          let attArr = null
          for (let i = 0; i < folderInfo.attribute.length; i++) {
            themeInfoArray[i] = new ThemesInfo()
            if (folderInfo.attribute[i] !== null) {
              themeInfoArray[i].LayerName = folderInfo.attribute[i].name
              attArr = JSON.parse(folderInfo.attribute[i].value) // [];
              if (attArr !== null && attArr.length > 0) {
                themeInfoArray[i].ThemeArr = [] // new ThemeBase[attArr.Length];
                for (let j = 0; j < attArr.length; j++) {
                  switch (attArr[j].name) {
                    case 'CMultiClassTheme': // 多表达式（多分段）专题图
                      themeInfoArray[i].ThemeArr[j] = new CMultiClassTheme()
                      break
                    case 'CSimpleTheme': // 简单专题图
                      themeInfoArray[i].ThemeArr[j] = new CSimpleTheme()
                      break
                    case 'CChartTheme': // 统计专题图
                      themeInfoArray[i].ThemeArr[j] = new CChartTheme()
                      break
                    case 'CGraduatedSymbolTheme': // 等级符号专题图
                      themeInfoArray[i].ThemeArr[j] = new CGraduatedSymbolTheme()
                      break
                    case 'CDotDensityTheme': /// /点密度专题图
                      themeInfoArray[i].ThemeArr[j] = new CDotDensityTheme()
                      break
                    case 'CRandomTheme': // 随机专题图
                      themeInfoArray[i].ThemeArr[j] = new CRandomTheme()
                      break
                    case 'CFourColorTheme': // 四色专题图
                      themeInfoArray[i].ThemeArr[j] = new CFourColorTheme()
                      break
                    case 'CUniqueTheme': // 唯一值专题图
                      themeInfoArray[i].ThemeArr[j] = new CUniqueTheme()
                      break
                    case 'CRangeTheme': // 范围专题图（分段专题图）
                      themeInfoArray[i].ThemeArr[j] = new CRangeTheme()
                      break
                    default:
                      themeInfoArray[i].ThemeArr[j] = new CSimpleTheme()
                  }
                  extendDeep(
                    themeInfoArray[i].ThemeArr[j],
                    JSON.parse(attArr[j].value)
                  )
                }
              }
            }

            return themeInfoArray
          }

          return undefined
        }
      }

      /** *
       * @description 查询专题图参数
       * @param {Object} options 查询参数
       * @param {String} [options.idxArr] 专题图索引(索引从0开始,例如："0,1,2/0,0,0"):图层索引/专题图索引，必填
       * @param {Function} [options.success] 查询成功回调
       * @param {Function} [options.failure] 查询失败回调
       * @example
       * // 回调方式
       * themeServer.queryThemeInfo({
       *   idxArr: '1/0',
       *   success: function (result) {
       *     console.log('请求成功：', result);
       *   }
       * });
       * // promise方式
       * themeServer.queryThemeInfo({
       *   idxArr: '1/0'
       * }).then(function (result) {
       *   console.log('请求成功：', result);
       * }).catch(function (result) {
       *   console.log('请求失败：', result);
       * })
       */
      queryThemeInfo(options) {
        options = defaultValue(options, {})

        const idxArr = defaultValue(options.idxArr, undefined)
        if (isNull(idxArr)) {
          Log.error('idxArr不能为空！')
          return
        }
        if (!(typeof idxArr === 'string')) {
          Log.error('idxArr类型错误！')
          return
        }

        const url = `${this.url}/get?idxArr=${idxArr}&guid=${this.guid}`
        return this._getRequest(url, options)
      }

      /**
       * 删除专题图
       * @param {Object} options 查询参数
       * @param {String} [options.idxArr] 专题图索引(索引从0开始,例如："0,1,2/0,0,0"):图层索引/专题图索引，必填
       * @param {Function} [options.success] 查询成功回调
       * @param {Function} [options.failure] 查询失败回调
       * @example
       * // 回调方式
       * themeServer.deleteTheme({
       *   idxArr: '1/0',
       *   success: function (result) {
       *     console.log('请求成功：', result);
       *   },
       *   failure: function (result) {
       *     console.log('请求失败：', result);
       *   }
       * });
       * // promise方式
       * themeServer.deleteTheme({
       *   idxArr: '1/0'
       * }).then(function (result) {
       *   console.log('请求成功：', result);
       * }).catch(function (result) {
       *   console.log('请求失败：', result);
       * });
       */
      deleteTheme(options) {
        options = defaultValue(options, {})

        const idxArr = defaultValue(options.idxArr, undefined)
        if (isNull(idxArr)) {
          Log.error('idxArr不能为空！')
          return
        }
        if (!(typeof idxArr === 'string')) {
          Log.error('idxArr类型错误！')
          return
        }

        const url = `${this.url}/remove?idxArr=${idxArr}&guid=${this.guid}`

        return this._getRequest(url, options)
      }

      /**
       * 更新专题图
       * @param {Object} options 查询参数
       * @param {String} [options.idxArr] 专题图索引(索引从0开始,例如："0,1,2/0,0,0"):图层索引/专题图索引，必填
       * @param {Array} [options.themeInfoArray] 专题图参数，必填
       * @param {Function} [options.success] 查询成功回调
       * @param {Function} [options.failure] 查询失败回调
       * @example
       * // 回调方式
       * // 更新专题图
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0].RegInfo.FillClr = 11;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1].RegInfo.FillClr = 16;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2].RegInfo.FillClr = 22;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3].RegInfo.FillClr = 26;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4].RegInfo.FillClr = 110;
       * themeServer.updateThemesInfo({
       *   idxArr: '1/0',
       *   themeInfoArray: themesInfoArr,
       *   success: function (result) {
       *     console.log('请求成功：', result);
       *   },
       *   failure: function (result) {
       *     console.log('请求失败：', result);
       *   }
       * });
       * // promise方式
       * themeServer.updateThemesInfo({
       *   idxArr: '1/0',
       *   themeInfoArray: themesInfoArr
       * }).then(function (result) {
       *   console.log('请求成功：', result);
       * }).catch(function (result) {
       *   console.log('请求失败：', result);
       * })
       */
      updateThemesInfo(options) {
        options = defaultValue(options, {})

        const idxArr = defaultValue(options.idxArr, undefined)
        if (isNull(idxArr)) {
          Log.error('idxArr不能为空！')
          return
        }
        if (!(typeof idxArr === 'string')) {
          Log.error('idxArr类型错误！')
          return
        }

        const themeInfoArray = defaultValue(options.themeInfoArray, undefined)
        if (isNull(themeInfoArray)) {
          Log.error('themeInfoArray不能为空！')
          return
        }
        if (!(themeInfoArray instanceof Array)) {
          Log.error('themeInfoArray类型错误！')
          return
        }

        const url = `${this.url}/update?idxArr=${idxArr}&guid=${this.guid}`
        const folderInfo = new FolderInfo()
        if (themeInfoArray.length > 0) {
          folderInfo.name = 'ThemeInfo'
          folderInfo.attribute = []
          for (let i = 0; i < themeInfoArray.length; i++) {
            folderInfo.attribute[i] = new FolderInfoAttribute()
            folderInfo.attribute[i].name = themeInfoArray[i].LayerName
            if (
              themeInfoArray[i].ThemeArr !== null &&
              themeInfoArray[i].ThemeArr.length > 0
            ) {
              const res = []
              for (let j = 0; j < themeInfoArray[i].ThemeArr.length; j++) {
                if (themeInfoArray[i].ThemeArr[j] !== null) {
                  res[j] = new FolderInfoAttribute(
                    themeInfoArray[i].ThemeArr[j].Type,
                    JSON.stringify(themeInfoArray[i].ThemeArr[j])
                  )
                }
              }
              folderInfo.attribute[i].value = JSON.stringify(res)
            }
          }
        }

        options.data = JSON.stringify(folderInfo)
        return this._postRequest(url, options)
      }

      /**
       * 添加专题图
       * @param {Object} options 查询参数
       * @param {String} [options.idxArr] 专题图索引(索引从0开始,例如："0,1,2/0,0,0"):图层索引/专题图索引，必填
       * @param {Array} [options.themeInfoArray] 专题图参数，必填
       * @param {Function} [options.success] 查询成功回调
       * @param {Function} [options.failure] 查询失败回调
       * @example
       * //回调方式
       * // ES5引入方式
       * const { Theme } = Zondy.Object
       * // ES6引入方式
       * import { Theme } from "@mapgis/webclient-common"
       * //专题图信息数组
       * var themesInfoArr = [];
       * //初始化Zondy.Object.Theme.ThemesInfo，用于设置需添加的专题相关信息
       * themesInfoArr[0] = new Theme.ThemesInfo();
       * //初始化指定图层的专题图信息对象，之后再给该数组赋值
       * themesInfoArr[0].LayerName = "湖北省市级区划2";
       * themesInfoArr[0].ThemeArr = [];
       * //实例化CMultiClassTheme类
       * themesInfoArr[0].ThemeArr[0] = new Theme.CRangeTheme();
       * themesInfoArr[0].ThemeArr[0].Name = "分段专题图";
       * //指定为分段专题图
       * themesInfoArr[0].ThemeArr[0].IsBaseTheme = false;
       * themesInfoArr[0].ThemeArr[0].Visible = true;
       * themesInfoArr[0].ThemeArr[0].GeoInfoType = "Reg";
       * //未分段值的图形信息设置
       * themesInfoArr[0].ThemeArr[0].DefaultInfo = new Theme.CThemeInfo();
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.Caption = "未分类";
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo = new Theme.CRegInfo();
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.Ovprnt = true;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.Angle = 0;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.EndClr = 0;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.FillClr = 17;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.FillMode = 0;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.FullPatFlg = true;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.PatClr = 45;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.PatHeight = 5;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.PatWidth = 5;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.PatID = 0;
       * themesInfoArr[0].ThemeArr[0].DefaultInfo.RegInfo.OutPenW = 1;
       * //分段取值设置
       * themesInfoArr[0].ThemeArr[0].Expression = "GDP2016";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr = [];
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0] = new Theme.CRangeThemeInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0].StartValue = "0";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0].EndValue = "100";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0].RegInfo = new Theme.CRegInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0].RegInfo.FillClr = 110;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1] = new Theme.CRangeThemeInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1].StartValue = "100";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1].EndValue = "150";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1].RegInfo = new Theme.CRegInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1].RegInfo.FillClr = 26;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2] = new Theme.CRangeThemeInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2].StartValue = "150";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2].EndValue = "200";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2].RegInfo = new Theme.CRegInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2].RegInfo.FillClr = 22;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3] = new Theme.CRangeThemeInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3].StartValue = "200";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3].EndValue = "400";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3].RegInfo = new Theme.CRegInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3].RegInfo.FillClr = 16;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4] = new Theme.CRangeThemeInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4].StartValue = "400";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4].EndValue = "6000";
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4].RegInfo = new Theme.CRegInfo();
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4].RegInfo.FillClr = 11;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[0].RegInfo.FillClr = 110;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[1].RegInfo.FillClr = 26;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[2].RegInfo.FillClr = 22;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[3].RegInfo.FillClr = 16;
       * themesInfoArr[0].ThemeArr[0].RangeThemeInfoArr[4].RegInfo.FillClr = 11;
       * themeServer.addTheme({
       *   idxArr: '1/0',
       *   themeInfoArray: themesInfoArr,
       *   success: function (result) {
       *     console.log('请求成功：', result);
       *   },
       *   failure: function (result) {
       *     console.log('请求失败：', result);
       *   }
       * });
       * //promise方式
       * themeServer.addTheme({
       *    idxArr: '1/0',
       *    themeInfoArray: themesInfoArr
       *  }).then(function (result) {
       *    console.log('请求成功：', result);
       *  }).catch(function (result) {
       *    console.log('请求失败：', result);
       *  });
       */
      addTheme(options) {
        options = defaultValue(options, {})

        const idxArr = defaultValue(options.idxArr, undefined)
        if (isNull(idxArr)) {
          Log.error('idxArr不能为空！')
          return
        }
        if (!(typeof idxArr === 'string')) {
          Log.error('idxArr类型错误！')
          return
        }

        const themeInfoArray = defaultValue(options.themeInfoArray, undefined)
        if (isNull(themeInfoArray)) {
          Log.error('themeInfoArray不能为空！')
          return
        }
        if (!(themeInfoArray instanceof Array)) {
          Log.error('themeInfoArray类型错误！')
          return
        }

        const url = `${this.url}/add?idxArr=${idxArr}&guid=${this.guid}`
        const folderInfo = new FolderInfo()
        if (themeInfoArray.length > 0) {
          folderInfo.name = 'ThemeInfo'
          folderInfo.attribute = []
          for (let i = 0; i < themeInfoArray.length; i++) {
            folderInfo.attribute[i] = new FolderInfoAttribute()
            folderInfo.attribute[i].name = themeInfoArray[i].LayerName
            if (
              themeInfoArray[i].ThemeArr !== null &&
              themeInfoArray[i].ThemeArr.length > 0
            ) {
              const res = []
              for (let j = 0; j < themeInfoArray[i].ThemeArr.length; j++) {
                if (themeInfoArray[i].ThemeArr[j] !== null) {
                  res[j] = new FolderInfoAttribute(
                    themeInfoArray[i].ThemeArr[j].Type,
                    JSON.stringify(themeInfoArray[i].ThemeArr[j])
                  )
                }
              }
              folderInfo.attribute[i].value = JSON.stringify(res)
            }
          }
        }

        options.data = JSON.stringify(folderInfo)
        return this._postRequest(url, options)
      }
    }

    export default ThemeServer
    Zondy.Service.ThemeServer = ThemeServer