import { Popup, PopupPropsType } from "@/components/Popup";
import React, { useState } from "react";
import ExportMnemonicPhrase from "./ExportMnemonicPhrase";
import SettingPanel, { SettingPanelProps } from "./SettingPanel";

type SettingPopupProps = Omit<SettingPanelProps, "openExportPhrase"> &
  Omit<PopupPropsType, "children">;

const SETTING_PANEL_HEIGHT = "346px";
const SETTING_EXPORT_PHRASE_HEIGHT = "528px";
const SettingPopup: React.FC<SettingPopupProps> = ({
  onChangePayPin,
  onClose,
  ...popupProps
}) => {
  const [showExport, setShowExport] = useState(false);
  const [height, setHeight] = useState(SETTING_PANEL_HEIGHT);
  const title = showExport ? "Export Mnemonic Phrase" : "Setting";

  const openExportPhrase = () => {
    setHeight(SETTING_EXPORT_PHRASE_HEIGHT);
    setShowExport(true);
  };
  const handleClose = () => {
    if (showExport) {
      setShowExport(false);
      setHeight(SETTING_PANEL_HEIGHT);
    } else {
      onClose?.();
    }
  };

  return (
    <Popup
      {...popupProps}
      close={showExport ? false : true}
      fixed={true}
      title={title}
      getContainer={() => document.body}
      onBack={handleClose}
      onClose={handleClose}
      bodyClassName="sm:uikit-max-w-[375px]"
      bodyStyle={{
        height,
      }}
    >
      {showExport ? (
        <ExportMnemonicPhrase />
      ) : (
        <SettingPanel
          onChangePayPin={onChangePayPin}
          openExportPhrase={openExportPhrase}
        />
      )}
    </Popup>
  );
};

export default SettingPopup;
