import React, {useCallback, useState} from "react";
import "../../../Assets/Styles/Auth/Auth.scss";
import {Button, Form, Input} from "antd";
import {authorize} from "../../../Store/Auth/Actions";
import {useTranslation} from "react-i18next";
import LocalePicker from "../../Misc/LocalePicker";
import {panelName} from "../../../Util/config";
import {useAppDispatch} from "../../../Hook/useAppDispatch";
import {useAppSelector} from "../../../Hook/useAppSelector";
import {AppState} from "../../../Store/RootReducer";
import Particles from "react-tsparticles";
import {loadFull} from "tsparticles";
import Mdicon from "../../Misc/Mdicon";

const LoginPage: React.FC = () => {
    const dispatch = useAppDispatch();
    const [form] = Form.useForm();
    const [loading, setLoading] = useState(false);

    const authReducer = useAppSelector((state: AppState) => state.auth);

    const authState = authReducer.formState;

    const authSubmit = () => {
        setLoading(true);

        dispatch(authorize(form.getFieldValue("token"), form.getFieldValue("password")));

        setTimeout(() => {
            setLoading(false);
        }, 1000);
    };

    const {t} = useTranslation();

    const particlesInit = useCallback(async (engine: any) => {
        console.log(engine);
        // you can initiate the tsParticles instance (engine) here, adding custom shapes or presets
        // this loads the tsparticles package bundle, it's the easiest method for getting everything ready
        // starting from v2 you can add only the features you need reducing the bundle size
        await loadFull(engine);
    }, []);

    const particlesLoaded = useCallback(async (container: any) => {
        await console.log(container);
    }, []);

    return (
        <>
            <div className="Auth-particles">
                <Particles
                    id="tsparticles"
                    init={particlesInit}
                    loaded={particlesLoaded}
                    options={{
                        particles: {
                            number: {
                                value: 6,
                                density: {
                                    enable: true,
                                    value_area: 800,
                                },
                            },
                            color: {
                                value: "#1b1e34",
                            },
                            shape: {
                                type: "polygon",
                                stroke: {
                                    width: 0,
                                    color: "#000",
                                },
                                polygon: {
                                    nb_sides: 6,
                                },
                                image: {
                                    src: "img/github.svg",
                                    width: 100,
                                    height: 100,
                                },
                            },
                            opacity: {
                                value: 0.3,
                                random: true,
                                anim: {
                                    enable: false,
                                    speed: 1,
                                    opacity_min: 0.1,
                                    sync: false,
                                },
                            },
                            size: {
                                value: 160,
                                random: false,
                                anim: {
                                    enable: true,
                                    speed: 10,
                                    size_min: 40,
                                    sync: false,
                                },
                            },
                            line_linked: {
                                enable: false,
                                distance: 200,
                                color: "#ffffff",
                                opacity: 1,
                                width: 2,
                            },
                            move: {
                                enable: true,
                                speed: 8,
                                direction: "none",
                                random: false,
                                straight: false,
                                out_mode: "out",
                                bounce: false,
                                attract: {
                                    enable: false,
                                    rotateX: 600,
                                    rotateY: 1200,
                                },
                            },
                        },
                        interactivity: {
                            detect_on: "canvas",
                            events: {
                                onhover: {
                                    enable: false,
                                    mode: "grab",
                                },
                                onclick: {
                                    enable: false,
                                    mode: "push",
                                },
                                resize: true,
                            },
                            modes: {
                                grab: {
                                    distance: 400,
                                    line_linked: {
                                        opacity: 1,
                                    },
                                },
                                bubble: {
                                    distance: 400,
                                    size: 40,
                                    duration: 2,
                                    opacity: 8,
                                    speed: 3,
                                },
                                repulse: {
                                    distance: 200,
                                    duration: 0.4,
                                },
                                push: {
                                    particles_nb: 4,
                                },
                                remove: {
                                    particles_nb: 2,
                                },
                            },
                        },
                        retina_detect: true,
                    }}
                />
            </div>
            <div className="Auth">
                <div>
                    <div className="Auth-inner">
                        <Form
                            className="Auth-Form"
                            layout="vertical"
                            onFinish={authSubmit}
                            form={form}
                            style={{width: 400}}
                        >
                            <div className="Auth-inner-title">
                                {panelName}<sup>v3.0</sup> login
                            </div>
                            {authState === "login" && (
                                <>
                                    <Form.Item label={t("Token")} name="token">
                                        <Input
                                            name="token"
                                            prefix={<Mdicon name="vpn_key"/>}
                                            placeholder={t("AUTH_TOKEN_PLACEHOLDER")}
                                            className="Auth-input"
                                            size="large"
                                        />
                                    </Form.Item>
                                    <Form.Item label={t("AUTH_CODEWORD")} name="password">
                                        <Input.Password
                                            name="password"
                                            prefix={<Mdicon name="password"/>}
                                            placeholder={t("AUTH_CODEWORD")}
                                            className="Auth-input"
                                            size="large"
                                        />
                                    </Form.Item>
                                </>
                            )}

                            {authState === "telegram" && (
                                <div className="auth-error-message">
                                    <h2 style={{lineHeight: 1}}>
                                        {t("LOGIN_ATTEMPT_LIMIT_TITLE")}
                                    </h2>

                                    <p>
                                        {t("LOGIN_ATTEMPT_ERROR_MESSAGE")}
                                    </p>

                                    <Form.Item name="authCheckCode" label={t("LOGIN_ATTEMPT_CODE_MESSAGE")}>
                                        <Input placeholder={t("ACTION_CODE")} className="ant-login-input"/>
                                    </Form.Item>
                                </div>
                            )}

                            {authState === "error" && (
                                <div className="auth-error-message">
                                    <h2 style={{lineHeight: 1}}>
                                        {t("LOGIN_ATTEMPT_LIMIT_TITLE")}
                                    </h2>

                                    <p>
                                        {t("LOGIN_ATTEMPT_ERROR_CONTACT_ADMIN")}
                                    </p>
                                </div>
                            )}

                            {authState !== "error" && (
                                <div className="Auth-footer">
                                    <LocalePicker/>

                                    <Button
                                        type="primary"
                                        className="Auth-Form-button"
                                        icon={<Mdicon name="login"/>}
                                        htmlType="submit"
                                        loading={loading}
                                        block
                                        size="large"
                                    >
                                        {t("AUTH_SIGN_IN")}
                                    </Button>
                                </div>
                            )}
                        </Form>
                    </div>
                </div>
            </div>
        </>
    );
};

export default LoginPage;
