function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { IconFont } from '@zgfe/business-lib';
import { Space, Switch } from 'antd';
import React, { useEffect, useState } from 'react';
import { classPrefix } from "./types";
var Controller = function Controller(_ref) {
  var client = _ref.client,
    phoneSize = _ref.phoneSize,
    container = _ref.container,
    status = _ref.status,
    onChange = _ref.onChange,
    onChangeShowControl = _ref.onChangeShowControl;
  var _useState = useState(100),
    _useState2 = _slicedToArray(_useState, 2),
    scale = _useState2[0],
    setScale = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    fresh = _useState4[0],
    setFresh = _useState4[1];
  var _useState5 = useState(localStorage.getItem('showAppVisualControl') ? true : false),
    _useState6 = _slicedToArray(_useState5, 2),
    showControl = _useState6[0],
    setShowControl = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    haveAutoFullscreen = _useState8[0],
    sethaveAutoFullscreen = _useState8[1];
  var step = 10;
  var max = 120;
  var min = 30;
  function onPageData() {
    setFresh(false);
  }
  useEffect(function () {
    if (onChange) onChange(scale);
  }, [scale]);
  useEffect(function () {
    if (onChangeShowControl) onChangeShowControl(showControl);
  }, [showControl]);
  useEffect(function () {
    if (!client) return;
    client.on('pageData', onPageData);
    return function () {
      client.removeListener('pageData', onPageData);
    };
  }, []);
  function onScale(flag) {
    var val = scale + step * flag;
    if (val < min) {
      setScale(min);
      return;
    }
    if (val > max) {
      setScale(max);
      return;
    }
    setScale(val);
  }
  function onFresh() {
    if (fresh) return;
    setFresh(true);
    client === null || client === void 0 ? void 0 : client.emit('fetchPageData');
  }
  function onFull() {
    if (!container || !container.current) return;
    var containerRect = container.current.getBoundingClientRect();
    var containerStyle = getComputedStyle(container.current);
    var paddingLeft = containerStyle.paddingLeft.match(/\d*/) ? Number(containerStyle.paddingLeft.match(/\d*/)[0]) : 0;
    var paddingRight = containerStyle.paddingRight.match(/\d*/) ? Number(containerStyle.paddingRight.match(/\d*/)[0]) : 0;
    var paddingTop = containerStyle.paddingTop.match(/\d*/) ? Number(containerStyle.paddingTop.match(/\d*/)[0]) : 0;
    var paddingBottom = containerStyle.paddingBottom.match(/\d*/) ? Number(containerStyle.paddingBottom.match(/\d*/)[0]) : 0;
    var width = containerRect.width - paddingLeft - paddingRight;
    var height = containerRect.height - paddingTop - paddingBottom - 6; // 6为防止溢出的误差值，随便定义的
    var targetScale = Math.ceil(Math.min(width / ((phoneSize === null || phoneSize === void 0 ? void 0 : phoneSize.width) || width), height / ((phoneSize === null || phoneSize === void 0 ? void 0 : phoneSize.height) || height)) * 100);
    setScale(targetScale);
  }
  useEffect(function () {
    if (haveAutoFullscreen || !phoneSize.height || !phoneSize.width) return;
    sethaveAutoFullscreen(true);
    onFull();
  }, [phoneSize]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-controller")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "shuaxin",
    className: "".concat(classPrefix, "-fresh ").concat(fresh ? 'loading' : ''),
    onClick: onFresh
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-status ").concat(status)
  }, status === 'success' ? '设备已连接' : 'APP状态：离线'), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-scal")
  }, /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(IconFont, {
    className: scale <= min ? 'disable' : '',
    type: "shanchu1",
    onClick: onScale.bind(undefined, -1)
  }), /*#__PURE__*/React.createElement("a", {
    style: {
      cursor: 'pointer',
      color: 'inherit'
    },
    onClick: setScale.bind(undefined, 100)
  }, "".concat(scale, "%")), /*#__PURE__*/React.createElement(IconFont, {
    className: scale >= max ? 'disable' : '',
    type: "tianjia",
    onClick: onScale.bind(undefined, 1)
  }), /*#__PURE__*/React.createElement(IconFont, {
    type: "quanpingzhanshi",
    onClick: onFull
  }))), /*#__PURE__*/React.createElement(Space, {
    size: 16
  }, /*#__PURE__*/React.createElement(Space, {
    align: "center"
  }, /*#__PURE__*/React.createElement("span", {
    className: "element defined"
  }), "\u5DF2\u57CB\u70B9\u4E8B\u4EF6"), /*#__PURE__*/React.createElement(Space, {
    align: "baseline"
  }, /*#__PURE__*/React.createElement("span", {
    className: "element definable"
  }), "\u53EF\u57CB\u70B9\u4E8B\u4EF6"), /*#__PURE__*/React.createElement(Space, {
    align: "baseline"
  }, /*#__PURE__*/React.createElement("span", {
    className: "element defining"
  }), "\u6B63\u5728\u57CB\u70B9\u4E8B\u4EF6"), /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(Switch, {
    onChange: function onChange(checked) {
      if (checked) {
        localStorage.setItem('showAppVisualControl', 'true');
      } else {
        localStorage.removeItem('showAppVisualControl');
      }
      setShowControl(checked);
    },
    defaultChecked: showControl
  }), "\u4E8B\u4EF6\u63A7\u5236\u5668")));
};
export default Controller;