import React, { useState } from 'react';
import { Dialogue, DialogueConfirm, Header, InfoField, Loading, PrimaryButton, deleteDevice, validateToken } from 'react-native-cloud-ca';
import { View, Image, Text } from 'react-native';
import styles from './styles';
import { useParams, useNavigate } from 'react-router-native';
const DeviceInfoView = props => {
  const {
    onDone
  } = props;
  let {
    device_id,
    device_name
  } = useParams();
  let navigate = useNavigate();
  const [isShowRequestDelete, setIsShowRequestDelete] = useState(false);
  const [isShowError, setIsShowError] = useState(false);
  const [isShowSuccess, setIsShowSuccess] = useState(false);
  const [errorResponse, setErrorResponse] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const [allResponse, setAllResponse] = useState();
  function onGoBack() {
    navigate(-1);
  }
  const handleShowRequestDelete = state => {
    setIsShowRequestDelete(state);
  };
  const onDeleteDevice = async () => {
    setIsLoading(true);
    try {
      await validateToken();
      const result = await deleteDevice({
        deviceId: device_id
      });
      setIsLoading(false);
      setIsShowRequestDelete(false);
      setIsShowSuccess(true);
      setAllResponse({
        deleteDeviceResponse: result
      });
    } catch (error) {
      setErrorResponse(`${error === null || error === void 0 ? void 0 : error.code} - ${error === null || error === void 0 ? void 0 : error.message}`);
      setAllResponse({
        error: error
      });
      setIsLoading(false);
      setIsShowRequestDelete(false);
      setIsShowError(true);
    }
  };
  const handleDone = () => {
    setIsShowSuccess(false);
    onDone === null || onDone === void 0 ? void 0 : onDone(allResponse);
    onGoBack();
  };
  const handleError = () => {
    setIsShowError(false);
    onDone === null || onDone === void 0 ? void 0 : onDone(allResponse);
  };
  return /*#__PURE__*/React.createElement(View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(Header, {
    label: "Chi ti\u1EBFt thi\u1EBFt b\u1ECB",
    goBack: onGoBack
  }), /*#__PURE__*/React.createElement(View, {
    style: styles.contentContainer
  }, /*#__PURE__*/React.createElement(Image, {
    style: styles.smartphoneImage,
    source: require('../../asset/icon/IC_SMARTPHONE.png')
  }), /*#__PURE__*/React.createElement(InfoField, {
    title: "ID",
    info: device_id
  }), /*#__PURE__*/React.createElement(InfoField, {
    title: "T\xEAn",
    info: device_name
  }), /*#__PURE__*/React.createElement(PrimaryButton, {
    label: "Hu\u1EF7 \u0111\u0103ng k\xFD",
    onPress: () => handleShowRequestDelete(true)
  })), /*#__PURE__*/React.createElement(Dialogue, {
    visible: isShowSuccess,
    onClose: handleDone
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, "Hu\u1EF7 thi\u1EBFt b\u1ECB th\xE0nh c\xF4ng")), /*#__PURE__*/React.createElement(Dialogue, {
    modalType: "ERROR",
    onClose: handleError,
    visible: isShowError
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, errorResponse)), /*#__PURE__*/React.createElement(DialogueConfirm, {
    modalType: "WARNING",
    title: "Th\xF4ng b\xE1o",
    closeOnPress: () => handleShowRequestDelete(false),
    closeLabel: "Quay l\u1EA1i",
    confirmOnPress: onDeleteDevice,
    confirmLabel: "H\u1EE7y \u0111\u0103ng k\xFD",
    visible: isShowRequestDelete
  }, /*#__PURE__*/React.createElement(Text, {
    style: styles.contentStyle
  }, "B\u1EA1n c\xF3 ch\u1EAFc mu\u1ED1n hu\u1EF7 \u0111\u0103ng k\xFD tr\xEAn thi\u1EBFt b\u1ECB n\xE0y kh\xF4ng?")), isLoading && /*#__PURE__*/React.createElement(Loading, null));
};
export default DeviceInfoView;
//# sourceMappingURL=DeviceInfoView.js.map