import { Button } from '@mui/material';
import { useCallback, useRef } from 'react';
import { useAppVersionStore } from '../store';
import { useShallow } from 'zustand/react/shallow';
import { message } from '@/modules/message';
import { useTranslation } from 'react-i18next';
export type FileType = {
  name: string;
  size: number;
  lastModified: number;
  webkitRelativePath: string; // 包含name
};

export const FileUpload = () => {
  const ref = useRef<HTMLInputElement | null>(null);
  const { t } = useTranslation();
  const appVersionStore = useAppVersionStore(
    useShallow((state) => {
      return {
        formData: state.formData,
        setFormData: state.setFormData,
        updateByFromData: state.updateByFromData,
      };
    }),
  );
  const onChange = useCallback(
    async (e) => {
      console.log(e.target.files);
      // webkitRelativePath
      let files = Array.from(e.target.files) as any[];
      console.log(files);
      if (files.length === 0) {
        message.error('请选择文件');
        return;
      }

      // 过滤 文件 .DS_Store
      files = files.filter((file) => {
        if (file.webkitRelativePath.startsWith('__MACOSX')) {
          return false;
        }
        // 过滤node_modules
        if (file.webkitRelativePath.includes('node_modules')) {
          return false;
        }
        // 过滤以.开头的文件
        return !file.name.startsWith('.');
      });
      if (files.length === 0) {
        console.log('no files');
        return;
      }
      const root = files[0].webkitRelativePath.split('/')[0];
      const formData = new FormData();
      files.forEach((file) => {
        // relativePath 去除第一级
        const webkitRelativePath = file.webkitRelativePath.replace(root + '/', '');
        formData.append('file', file, webkitRelativePath); // 保留文件夹路径
      });
      const key = appVersionStore.formData.key;
      const version = appVersionStore.formData.version;
      formData.append('appKey', key);
      formData.append('version', version);
      const res = await fetch('/api/app/upload', {
        method: 'POST',
        body: formData, //
        headers: {
          Authorization: 'Bearer ' + localStorage.getItem('token'),
        },
      }).then((res) => res.json());
      if (res?.code === 200) {
        appVersionStore.setFormData(res.data);
        appVersionStore.updateByFromData();
      } else {
        message.error(res.message || 'Request failed');
      }
      // 清理之前上传的文件
      e.target.value = '';
    },
    [appVersionStore.formData],
  );

  return (
    <div>
      <input
        className='hidden'
        ref={ref}
        type='file'
        // @ts-ignore
        webkitdirectory='true'
        multiple
        onChange={onChange}
      />
      <Button
        onClick={() => {
          const key = appVersionStore.formData.key;
          const version = appVersionStore.formData.version;
          if (!key || !version) {
            message.error('请先选择应用和版本');
            return;
          }
          ref.current!.click();
        }}>
        {t('uploadDirectory')}
      </Button>
    </div>
  );
};
