import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { API, isMobile, showError, showSuccess, renderQuota, renderQuotaWithPrompt } from '../../helpers';
import {
  Button,
  Input,
  Modal,
  Select,
  SideSheet,
  Space,
  Spin,
  Typography,
  Card,
  Tag,
  Avatar,
} from '@douyinfe/semi-ui';
import {
  IconUser,
  IconSave,
  IconClose,
  IconLink,
  IconUserGroup,
  IconPlus,
} from '@douyinfe/semi-icons';
import { useTranslation } from 'react-i18next';

const { Text, Title } = Typography;

const EditUser = (props) => {
  const userId = props.editingUser.id;
  const [loading, setLoading] = useState(true);
  const [addQuotaModalOpen, setIsModalOpen] = useState(false);
  const [addQuotaLocal, setAddQuotaLocal] = useState('');
  const [inputs, setInputs] = useState({
    username: '',
    display_name: '',
    password: '',
    github_id: '',
    oidc_id: '',
    wechat_id: '',
    email: '',
    quota: 0,
    group: 'default',
    remark: '',
  });
  const [groupOptions, setGroupOptions] = useState([]);
  const {
    username,
    display_name,
    password,
    github_id,
    oidc_id,
    wechat_id,
    telegram_id,
    email,
    quota,
    group,
    remark,
  } = inputs;
  const handleInputChange = (name, value) => {
    setInputs((inputs) => ({ ...inputs, [name]: value }));
  };
  const fetchGroups = async () => {
    try {
      let res = await API.get(`/api/group/`);
      setGroupOptions(
        res.data.data.map((group) => ({
          label: group,
          value: group,
        })),
      );
    } catch (error) {
      showError(error.message);
    }
  };
  const navigate = useNavigate();
  const handleCancel = () => {
    props.handleClose();
  };
  const loadUser = async () => {
    setLoading(true);
    let res = undefined;
    if (userId) {
      res = await API.get(`/api/user/${userId}`);
    } else {
      res = await API.get(`/api/user/self`);
    }
    const { success, message, data } = res.data;
    if (success) {
      data.password = '';
      setInputs(data);
    } else {
      showError(message);
    }
    setLoading(false);
  };

  useEffect(() => {
    loadUser().then();
    if (userId) {
      fetchGroups().then();
    }
  }, [props.editingUser.id]);

  const submit = async () => {
    setLoading(true);
    let res = undefined;
    if (userId) {
      let data = { ...inputs, id: parseInt(userId) };
      if (typeof data.quota === 'string') {
        data.quota = parseInt(data.quota);
      }
      res = await API.put(`/api/user/`, data);
    } else {
      res = await API.put(`/api/user/self`, inputs);
    }
    const { success, message } = res.data;
    if (success) {
      showSuccess('用户信息更新成功！');
      props.refresh();
      props.handleClose();
    } else {
      showError(message);
    }
    setLoading(false);
  };

  const addLocalQuota = () => {
    let newQuota = parseInt(quota) + parseInt(addQuotaLocal);
    setInputs((inputs) => ({ ...inputs, quota: newQuota }));
  };

  const openAddQuotaModal = () => {
    setAddQuotaLocal('0');
    setIsModalOpen(true);
  };

  const { t } = useTranslation();

  return (
    <>
      <SideSheet
        placement={'right'}
        title={
          <Space>
            <Tag color="blue" shape="circle">{t('编辑')}</Tag>
            <Title heading={4} className="m-0">
              {t('编辑用户')}
            </Title>
          </Space>
        }
        headerStyle={{
          borderBottom: '1px solid var(--semi-color-border)',
          padding: '24px'
        }}
        bodyStyle={{ padding: '0' }}
        visible={props.visible}
        width={isMobile() ? '100%' : 600}
        footer={
          <div className="flex justify-end bg-white">
            <Space>
              <Button
                theme="solid"
                className="!rounded-full"
                onClick={submit}
                icon={<IconSave />}
                loading={loading}
              >
                {t('提交')}
              </Button>
              <Button
                theme="light"
                className="!rounded-full"
                type="primary"
                onClick={handleCancel}
                icon={<IconClose />}
              >
                {t('取消')}
              </Button>
            </Space>
          </div>
        }
        closeIcon={null}
        onCancel={() => handleCancel()}
      >
        <Spin spinning={loading}>
          <div className="p-6">
            <Card className="!rounded-2xl shadow-sm border-0 mb-6">
              {/* Header: Basic Info */}
              <div className="flex items-center mb-2">
                <Avatar size="small" color="blue" className="mr-2 shadow-md">
                  <IconUser size={16} />
                </Avatar>
                <div>
                  <Text className="text-lg font-medium">{t('基本信息')}</Text>
                  <div className="text-xs text-gray-600">{t('用户的基本账户信息')}</div>
                </div>
              </div>

              <div className="space-y-4">
                <div>
                  <Text strong className="block mb-2">{t('用户名')}</Text>
                  <Input
                    placeholder={t('请输入新的用户名')}
                    onChange={(value) => handleInputChange('username', value)}
                    value={username}
                    autoComplete="new-password"
                    className="!rounded-lg"
                    showClear
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('密码')}</Text>
                  <Input
                    type="password"
                    placeholder={t('请输入新的密码，最短 8 位')}
                    onChange={(value) => handleInputChange('password', value)}
                    value={password}
                    autoComplete="new-password"
                    className="!rounded-lg"
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('显示名称')}</Text>
                  <Input
                    placeholder={t('请输入新的显示名称')}
                    onChange={(value) => handleInputChange('display_name', value)}
                    value={display_name}
                    autoComplete="new-password"
                    className="!rounded-lg"
                    showClear
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('备注')}</Text>
                  <Input
                    placeholder={t('请输入备注（仅管理员可见）')}
                    onChange={(value) => handleInputChange('remark', value)}
                    value={remark}
                    autoComplete="off"
                    className="!rounded-lg"
                    showClear
                  />
                </div>
              </div>
            </Card>

            {userId && (
              <Card className="!rounded-2xl shadow-sm border-0 mb-6">
                {/* Header: Permission Settings */}
                <div className="flex items-center mb-2">
                  <Avatar size="small" color="green" className="mr-2 shadow-md">
                    <IconUserGroup size={16} />
                  </Avatar>
                  <div>
                    <Text className="text-lg font-medium">{t('权限设置')}</Text>
                    <div className="text-xs text-gray-600">{t('用户分组和额度管理')}</div>
                  </div>
                </div>

                <div className="space-y-4">
                  <div>
                    <Text strong className="block mb-2">{t('分组')}</Text>
                    <Select
                      placeholder={t('请选择分组')}
                      search
                      allowAdditions
                      additionLabel={t(
                        '请在系统设置页面编辑分组倍率以添加新的分组：',
                      )}
                      onChange={(value) => handleInputChange('group', value)}
                      value={inputs.group}
                      autoComplete="new-password"
                      optionList={groupOptions}
                      className="w-full !rounded-lg"
                    />
                  </div>

                  <div>
                    <div className="flex justify-between mb-2">
                      <Text strong>{t('剩余额度')}</Text>
                      <Text type="tertiary">{renderQuotaWithPrompt(quota)}</Text>
                    </div>
                    <div className="flex gap-2">
                      <Input
                        placeholder={t('请输入新的剩余额度')}
                        onChange={(value) => handleInputChange('quota', value)}
                        value={quota}
                        type="number"
                        autoComplete="new-password"
                        className="flex-1 !rounded-lg"
                      />
                      <Button
                        onClick={openAddQuotaModal}
                        className="!rounded-lg"
                        icon={<IconPlus />}
                      >
                        {t('添加额度')}
                      </Button>
                    </div>
                  </div>
                </div>
              </Card>
            )}

            <Card className="!rounded-2xl shadow-sm border-0">
              {/* Header: Bindings */}
              <div className="flex items-center mb-2">
                <Avatar size="small" color="purple" className="mr-2 shadow-md">
                  <IconLink size={16} />
                </Avatar>
                <div>
                  <Text className="text-lg font-medium">{t('绑定信息')}</Text>
                  <div className="text-xs text-gray-600">{t('第三方账户绑定状态（只读）')}</div>
                </div>
              </div>

              <div className="space-y-4">
                <div>
                  <Text strong className="block mb-2">{t('已绑定的 GitHub 账户')}</Text>
                  <Input
                    value={github_id}
                    autoComplete="new-password"
                    placeholder={t(
                      '此项只读，需要用户通过个人设置页面的相关绑定按钮进行绑定，不可直接修改',
                    )}
                    readonly
                    className="!rounded-lg"
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('已绑定的 OIDC 账户')}</Text>
                  <Input
                    value={oidc_id}
                    placeholder={t(
                      '此项只读，需要用户通过个人设置页面的相关绑定按钮进行绑定，不可直接修改',
                    )}
                    readonly
                    className="!rounded-lg"
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('已绑定的微信账户')}</Text>
                  <Input
                    value={wechat_id}
                    autoComplete="new-password"
                    placeholder={t(
                      '此项只读，需要用户通过个人设置页面的相关绑定按钮进行绑定，不可直接修改',
                    )}
                    readonly
                    className="!rounded-lg"
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('已绑定的邮箱账户')}</Text>
                  <Input
                    value={email}
                    autoComplete="new-password"
                    placeholder={t(
                      '此项只读，需要用户通过个人设置页面的相关绑定按钮进行绑定，不可直接修改',
                    )}
                    readonly
                    className="!rounded-lg"
                  />
                </div>

                <div>
                  <Text strong className="block mb-2">{t('已绑定的 Telegram 账户')}</Text>
                  <Input
                    value={telegram_id}
                    autoComplete="new-password"
                    placeholder={t(
                      '此项只读，需要用户通过个人设置页面的相关绑定按钮进行绑定，不可直接修改',
                    )}
                    readonly
                    className="!rounded-lg"
                  />
                </div>
              </div>
            </Card>
          </div>
        </Spin>
      </SideSheet>

      <Modal
        centered={true}
        visible={addQuotaModalOpen}
        onOk={() => {
          addLocalQuota();
          setIsModalOpen(false);
        }}
        onCancel={() => setIsModalOpen(false)}
        closable={null}
        title={
          <div className="flex items-center">
            <IconPlus className="mr-2" />
            {t('添加额度')}
          </div>
        }
      >
        <div className="mb-4">
          <Text type="secondary" className="block mb-2">
            {`${t('新额度')}${renderQuota(quota)} + ${renderQuota(addQuotaLocal)} = ${renderQuota(quota + parseInt(addQuotaLocal || 0))}`}
          </Text>
        </div>
        <Input
          placeholder={t('需要添加的额度（支持负数）')}
          onChange={(value) => {
            setAddQuotaLocal(value);
          }}
          value={addQuotaLocal}
          type="number"
          autoComplete="new-password"
          className="!rounded-lg"
        />
      </Modal>
    </>
  );
};

export default EditUser;
