import _regeneratorRuntime from 'babel-runtime/regenerator';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

import jsonp from './../jsonp';
import Device from './../Device';
import MapUtil from './../MapUtil';
import Toast from './../Toast/instance.js';
import Alert from './../Alert/instance.js';
import Loading from './../Loading/instance.js';
import locale from './../locale';

// 防止绑定事件时this指向window, 所以全局加一个变量用于存储this
window._bridge_self = null;

var Bridge = {
  /**
  * 基础功能:start
  */
  debug: false,
  // 拨打电话
  tel: function tel(number) {
    var self = this;
    if (Device.device === 'pc') {
      self.showToast(locale('hint_only_mobile') || '此功能仅可在手机中使用', { mask: false });
      return;
    }
    if (isNaN(number)) return;
    window.location.href = 'tel:' + number;
  },
  // 弹出toast
  toast: null,
  showToast: function showToast(msg) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var self = this;
    if (!msg) return;
    if (!self.toast) {
      // 提示错误
      self.toast = new Toast({
        parent: document.body,
        maskClass: 'mask toast-mask' + (params.mask === false ? ' toast-propagation' : ''),
        toastClass: 'toast ' + (params.position ? params.position : 'middle'),
        icon: params.icon || '',
        delay: params.delay || 2000,
        html: msg
      });
    } else {
      self.toast.updateParams(_extends({}, params, {
        maskClass: 'mask toast-mask' + (params.mask === false ? ' toast-propagation' : ''),
        toastClass: 'toast ' + (params.position ? params.position : 'middle'),
        icon: params.icon || '',
        delay: params.delay || 2000,
        html: msg
      }));
    }
    self.toast.show();
    if (params.success) {
      setTimeout(function () {
        params.success();
      }, params.delay ? Math.round(params.delay / 2) : 1000);
    }
  },
  // 弹出loading
  loading: null,
  showLoading: function showLoading() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var self = this;
    if (!self.loading) {
      self.loading = new Loading(_extends({}, params, {
        caption: params.caption || locale('loading') || '正在加载...',
        maskClass: 'mask loading-mask ' + (params.mask === false ? ' loading-propagation' : '')
      }));
    } else {
      self.loading.updateParams(_extends({}, params, {
        caption: params.caption || locale('loading') || '正在加载...',
        maskClass: 'mask loading-mask ' + (params.mask === false ? ' loading-propagation' : '')
      }));
    }
    self.loading.show();
  },
  hideLoading: function hideLoading() {
    var self = this;
    if (self.loading) {
      self.loading.hide();
    }
  },
  // 弹出Alert
  alert: null,
  showAlert: function showAlert(msg) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var self = this;
    if (!self.alert) {
      self.alert = new Alert(_extends({}, params, {
        html: msg,
        buttonSubmitHTML: locale('ok') || '确定', // 实例化时需要国际化
        buttonCancelHTML: locale('cancel') || '取消', // 实例化时需要国际化
        onClickSubmit: function onClickSubmit(e) {
          if (params.success) params.success(e);else e.hide();
        }
      }));
    } else {
      if (params) {
        self.alert.updateParams(_extends({
          buttonSubmitHTML: locale('ok') || '确定', // 实例化时需要国际化
          buttonCancelHTML: locale('cancel') || '取消' }, params, {
          html: msg
        }));
      }
    }
    self.alert.show();
  },
  // 弹出Confirm
  confirm: null,
  showConfirm: function showConfirm(msg) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var self = this;
    if (!self.confirm) {
      self.confirm = new Alert(_extends({}, params, {
        html: msg,
        onClickSubmit: function onClickSubmit(e) {
          if (params.success) params.success(e);
        },
        onClickCancel: function onClickCancel(e) {
          e.errMsg = '';
          if (params.fail) params.fail(e);else e.hide();
        }
      }));
    } else {
      if (params) {
        self.confirm.updateParams(_extends({
          buttonSubmitHTML: locale('ok') || '确定', // 实例化时需要国际化
          buttonCancelHTML: locale('cancel') || '取消' }, params, {
          html: msg,
          onClickSubmit: function onClickSubmit(e) {
            if (params.success) params.success(e);
          },
          onClickCancel: function onClickCancel(e) {
            e.errMsg = '';
            if (params.fail) params.fail(e);else e.hide();
          }
        }));
      }
    }
    self.confirm.show();
  },
  getLocationTask: function getLocationTask(res) {
    // 记录定位任务, 防止重复定位
    var self = this;
    if (self.locationTask && self.locationTask.length) {
      for (var _iterator = self.locationTask, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
        var _ref;

        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref = _i.value;
        }

        var locationTaskItem = _ref;

        if (res.longitude && res.latitude) {
          if (locationTaskItem.success) locationTaskItem.success(res);
        } else {
          if (locationTaskItem.fail) locationTaskItem.fail(res);
        }
      }
    }
    self.locationTask = null;
  },
  /**
    * 百度地图:获取当前位置名称
    * @param {Object} params: {longitude: '', latitude: '', success: fn, fail: fn}
    * @param {String} type 'wgs84 | gcj02', 默认gcj02
    * @return {Promise} result: {status: 0 成功, points 百度坐标}
    */
  getAddress: function getAddress() {
    var _this = this;

    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'gcj02';

    return new Promise(function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(resolve) {
        var mapUtil, result;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                mapUtil = new MapUtil();

                if (window.BMap) {
                  _context.next = 4;
                  break;
                }

                resolve(null);
                return _context.abrupt('return');

              case 4:
                _context.next = 6;
                return mapUtil.getAddress([params.longitude, params.latitude], type);

              case 6:
                result = _context.sent;

                resolve(result);

              case 8:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, _this);
      }));

      return function (_x7) {
        return _ref2.apply(this, arguments);
      };
    }());
  },
  /**
    * 百度地图:获得天气
    * @param {Object} params: {location: 'lng,lat|lng,lat|lng,lat' | '北京市|上海市', success: fn, fail: fn}
    * @returns {Object} 天气信息results
    */
  getWeather: function getWeather() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var url = 'http://api.map.baidu.com/telematics/v3/weather?location=' + (params.location || '南京市') + '&output=json&ak=IlfRglMOvFxapn5eGrmAj65H';
    jsonp(url, null, function (err, data) {
      if (err) {
        if (params.fail) params.fail({ errMsg: 'getWeather:' + (locale('hint_weather_failed') || '获取天气失败, 请稍后重试') + err });
      } else {
        if (data.results && data.results.length) {
          if (params.success) params.success(data.results);
        } else {
          if (params.fail) params.fail({ errMsg: 'getWeather:' + (locale('hint_weather_failed') || '获取天气失败, 请稍后重试') });
        }
      }
    });
  },
  // 客户端默认返回控制
  back: function back(argHistory, argBackLvl) {
    // 因为有可能是监听绑定, this指向有可能是window, 所以需要指定self
    var self = _bridge_self;
    // 返回操作对象与返回层级
    var _history = window.history;
    if (argHistory && argHistory.go) _history = argHistory;
    var _backLvl = argBackLvl || -1;

    // 返回类型
    var isFromApp = Device.getUrlParameter('isFromApp', location.search) || '';
    if (isFromApp === '1') {
      // 关闭当前页面
      try {
        self.closeWindow();
      } catch (error) {
        console.log(error);
      }
    } else if (isFromApp === 'home') {
      // 返回首页
      try {
        self.goHome();
      } catch (error) {
        console.log(error);
      }
    } else if (isFromApp === 'confirm') {
      // 提示后返回上一页
      self.showConfirm(self.confirmCaption || locale('confirm_quit_page') || '您确定要离开此页面吗?', {
        success: function success(e) {
          e.hide();
          _history.go(_backLvl);
        }
      });
    } else if (isFromApp === 'confirm-close') {
      // 提示后关闭当前页面
      self.showConfirm(self.confirmCaption || locale('confirm_quit_page') || '您确定要离开此页面吗?', {
        success: function success(e) {
          e.hide();
          self.closeWindow();
        }
      });
    } else if (isFromApp === 'custom') {
      console.log('自定义');
    } else {
      // 返加上一页
      _history.go(_backLvl);
    }
  },
  /**
    * 动态加载桥接库
    * @param {Func} callback 加载完成回调
    * @param {Object} options {wxSrc: '', wqCordovaSrc: '外勤cordovajs', wqSrc: '外勤jssdkjs', fail: func({errMsg: ''})}
    */
  ready: function ready(callback) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var self = this;
    var platform = self.platform;
    if (platform !== 'wechat' && platform !== 'wework' && platform !== 'miniprogram' && platform !== 'waiqin' && platform !== 'dinghuo' && platform !== 'wq') {
      if (callback) window.addEventListener('load', callback, false);
      return;
    }
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.defer = 'defer';
    if (platform === 'wechat' || platform === 'wework' || platform === 'miniprogram') {
      // 微信
      script.src = options.wxSrc || '//res.wx.qq.com/open/js/jweixin-1.6.0.js';
      if (callback) {
        script.onload = function () {
          callback();
        };
      }
      if (options.fail) {
        script.onerror = function () {
          options.fail({ errMsg: '微信js加载失败' });
        };
      }
    } else if (platform === 'waiqin') {
      // 外勤cordova
      script.src = options.wqCordovaSrc || '//res.waiqin365.com/d/common_mobile/component/cordova/cordova.js';
      if (callback) {
        script.onload = function () {
          document.addEventListener('deviceready', function () {
            callback();
            self.config();
          });
        };
      }
      if (options.fail) {
        script.onerror = function () {
          options.fail({ errMsg: '外勤cordova加载失败' });
        };
      }
    } else if (platform === 'wq') {
      // 外勤jssdk
      // 用开发d目录可以使用新功能
      script.src = options.wqSrc || '//res.waiqin365.com/d/open/js/waiqin365.min.js?v=1.0.1';
      if (callback) {
        script.onload = function () {
          callback();
          self.config();
        };
      }
      if (options.fail) {
        script.onerror = function () {
          options.fail({ errMsg: '外勤js加载失败' });
        };
      }
    } else if (platform === 'dinghuo') {
      callback();
      self.config();
    }
    if (script.src) document.body.appendChild(script);
  },
  /**
    * 修改原生标题
    * @param {Object} params {title: '自定义标题'}
    */
  setTitle: function setTitle(params) {
    if (params && params.title) document.title = params.title;
  }
  /**
   * 基础功能:end
   */
};
export default Bridge;