'use client';

import React, { FC, useEffect, useState } from 'react';
import { Form, Upload, Button } from 'antd';
import type { UploadFile, UploadProps } from 'antd';
import ImgCrop from 'antd-img-crop';
import { CameraOutlined } from '@ant-design/icons';
import { Link } from '../../navigation';
import { CustomInput } from '../view/CustomInput';
import { Profile } from '../../types/profile';
import { useProfileSettings } from '../../actions/hooks/useProfileSettings';
import { i18nText } from '../../i18nKeys';

type ProfileSettingsProps = {
    locale: string;
};

// type FileType = Parameters<GetProp<UploadProps, 'beforeUpload'>>[0];

export const ProfileSettings: FC<ProfileSettingsProps> = ({ locale }) => {
    const [form] = Form.useForm<Profile>();
    const { profileSettings } = useProfileSettings(locale);

    useEffect(() => {
        if (profileSettings) {
            form.setFieldsValue(profileSettings);
        }
    }, [profileSettings]);

    const [fileList, setFileList] = useState<UploadFile[]>();

    const onChange: UploadProps['onChange'] = ({ fileList: newFileList }) => {
        setFileList(newFileList);
    };

    const onPreview = async (file: UploadFile) => {
        // let src = file.url as string;
        // if (!src) {
        //     src = await new Promise((resolve) => {
        //         const reader = new FileReader();
        //         reader.readAsDataURL(file.originFileObj as FileType);
        //         reader.onload = () => resolve(reader.result as string);
        //     });
        // }
        // const image = new Image();
        // image.src = src;
        // const imgWindow = window.open(src);
        // imgWindow?.document.write(image.outerHTML);
    };

    return (
        <Form form={form} className="form-settings">
            <div className="user-avatar">
                <div className="user-avatar__edit" style={profileSettings?.faceImageUrl ? { backgroundImage: `url(${profileSettings.faceImageUrl})` } : undefined}>
                    <input className="" type="file" id="input-file" />
                    <label htmlFor="input-file" className="form-label" />
                </div>
                <div className="user-avatar__text">{i18nText('photoDesc', locale)}</div>
            </div>
            {/* <ImgCrop rotationSlider>
                <Upload
                    action="https://run.mocky.io/v3/435e224c-44fb-4773-9faf-380c5e6a2188"
                    fileList={fileList}
                    onChange={onChange}
                    onPreview={onPreview}
                >
                    <Button icon={<CameraOutlined />}>Click to Upload</Button>
                </Upload>
            </ImgCrop> */}
            <div className="form-field">
                <Form.Item name="username">
                    <CustomInput placeholder={i18nText('name', locale)} />
                </Form.Item>
            </div>
            <div className="form-field">
                <Form.Item name="surname">
                    <CustomInput placeholder={i18nText('surname', locale)} />
                </Form.Item>
            </div>
            {/* <div className="form-field">
                <Form.Item name="birthday">
                    <CustomInput placeholder={i18nText('birthday', locale)} />
                </Form.Item>
            </div> */}
            <div className="form-field">
                <Form.Item name="login">
                    <CustomInput type="email" placeholder="E-mail" />
                </Form.Item>
            </div>
            <div className="form-link">
                <Link href={'change-password' as any}>
                    {i18nText('changePass', locale)}
                </Link>
            </div>
            <button className="btn-apply">{i18nText('save', locale)}</button>
        </Form>
    );
};
