import React, {Dispatch, SetStateAction, useState} from "react";
import {Button, Checkbox, Col, Divider, Space, Tag} from "antd";
import {Bot} from "../../../Model/Bot";
import {AppState} from "../../../Store/RootReducer";
import moment from "moment";
import {humanizeBotLastConnection} from "../../../Util/humanizeBotLastConnection";
import Permissions from "./Permissions";
import BotDataButtons from "./BotDataButtons";
import Injections from "./Injections";
import {changeSelectedBot} from "../../../Store/Bots/Actions";
import Actions from "./Actions";
import {useTranslation} from "react-i18next";
import BotInfo from "../../Modals/BotInfo/BotInfo";
import {NavLink} from "react-router-dom";
import {useAppDispatch} from "../../../Hook/useAppDispatch";
import {useAppSelector} from "../../../Hook/useAppSelector";
import useBreakpoint from "antd/es/grid/hooks/useBreakpoint";
import {LogButtonsProps} from "../../../Model/Commands";

interface BotsItemProps {
    bot: Bot,
    isLoaded: boolean,
    bots: Bot[],
    logsList: LogButtonsProps[],
    setSelectedBot: Dispatch<SetStateAction<Bot>>,
    setShowModal: (modal: string, show: boolean) => void,
}

const BotsItem: React.FC<BotsItemProps> = (props: BotsItemProps) => {
    const lastConnectionDiff = moment().diff(moment(props.bot.updated_at)) / 1000;
    const lastConnectionStatus = lastConnectionDiff <= 60 ? "online" : (lastConnectionDiff >= 144000 ? "uninstalled" : "offline");
    const botsReducer = useAppSelector((state: AppState) => state.bots);
    // const authReducer = useAppSelector((state: AppState) => state.auth);
    const isSelected = botsReducer.selectedBots.filter((bot: Bot) => {
        return props.bot.id === bot.id;
    }).length > 0;
    const dispatch = useAppDispatch();
    const {t} = useTranslation();
    const [showBotInfo, setShowBotInfo] = useState(false);
    const [botData, setBotData] = useState<Bot>();
    const showBotInfoModal = (bot: Bot) => {
        setBotData(bot);
        setShowBotInfo(true);
    };

    const screens = useBreakpoint();

    return (
        <Col xxl={6} xl={12} lg={12} md={12} sm={24} xs={24} className={isSelected ? "bot-col selected" : "bot-col"}>
            <div className={`bot-item ${lastConnectionStatus}`}>
                <div>
                    <div className="bot-item-top">
                        <div className={"bot-item-icon"}>
                            <span className="icon-bot-phone" style={{fontSize: 30}} />
                        </div>

                        <div className="bot-item-info" onClick={() => setShowBotInfo(true)}>
                            <div className="bot-item-name" onClick={() => showBotInfoModal(props.bot)}>
                                {props.bot.id}
                            </div>

                            <div className={`bot-item-status ${lastConnectionStatus}`}>{lastConnectionStatus}</div>
                        </div>

                        <div className={isSelected ? "bot-item-select selected" : "bot-item-select"}>
                            <Checkbox
                                style={{transform: "scale(1.5)"}}
                                checked={isSelected}
                                onChange={() => dispatch(changeSelectedBot(props.bot))}
                            />
                        </div>
                    </div>

                    <div style={{marginTop: 30}}>
                        {/* <Divider orientation="center" style={{margin: "5px 0"}}>*/}
                        {/*    {t("BOT_PERMISSIONS")}*/}
                        {/* </Divider>*/}
                        <Permissions bot={props.bot}/>
                    </div>

                    <div className="bot-item-tags">
                        <Divider orientation="center" style={{margin: "5px 0"}}>
                            <Tag color="#177ddc" style={{marginRight: 0}}>{props.bot.tag}</Tag>
                        </Divider>
                    </div>

                    <div>
                        {/* <Divider orientation="center" style={{margin: "5px 0"}}>*/}
                        {/*    {t("BOT_LOGS")}*/}
                        {/* </Divider>*/}
                        <BotDataButtons
                            bot={props.bot}
                            logsList={props.logsList}
                            setSelectedBot={props.setSelectedBot}
                            setShowModal={props.setShowModal}
                        />
                    </div>

                    <div className="bot-item-body">
                        <div className="bot-item-row">
                            <div className="bot-item-status-inner">
                                <div>
                                    <Divider orientation="center" style={{marginBottom: 0}}>
                                        {t("BOT_LAST_ONLINE")}: {props.bot.last_connection ? props.bot.last_connection : t("BOT_LAST_ONLINE_NO_DATA")}
                                        <br/>
                                        {t("BOT_INFECTED_AT")}: {props.bot.created_at ? props.bot.created_at : t("BOT_LAST_ONLINE_NO_DATA")}
                                    </Divider>
                                    <div style={{display: "flex", justifyContent: "center"}}>
                                        <Tag>
                                            {humanizeBotLastConnection(lastConnectionDiff)}
                                        </Tag>
                                    </div>
                                    <div style={{display: "flex", justifyContent: "center"}}>
                                        {screens.xs && (
                                            <>
                                                {lastConnectionStatus === "uninstalled" || lastConnectionStatus === "offline" ? (
                                                    <Checkbox className="ant-checkbox-tg">
                                                        {t("NOTIFY_TELEGRAM")}
                                                    </Checkbox>
                                                ) : ""}
                                            </>
                                        )}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div className="bot-item-row">
                            <Divider style={{marginBottom: 0}}>
                                {t("BOT_GEOLOCATION")}
                            </Divider>
                            <Space direction="vertical">
                                <div>
                                    <Space align="baseline">
                                        <i className="fa-solid fa-mobile" style={{fontSize: 22}}/>
                                        <span>
                                            {t("BOT_MODEL")}: {props.bot.metadata.model}
                                        </span>
                                    </Space>
                                </div>
                                {props.bot.comment && (
                                    <div>
                                        <Space align="baseline">
                                            <i className="fa-solid fa-mobile-screen" style={{fontSize: 22}}/>
                                            <span>
                                                {props.bot.comment}
                                            </span>
                                        </Space>
                                    </div>
                                )}
                                <Space>
                                    <img
                                        width={16}
                                        alt={props.bot.country_code.toUpperCase()}
                                        src={`https://purecatamphetamine.github.io/country-flag-icons/3x2/${props.bot.country_code.toUpperCase()}.svg`}
                                    />

                                    <span>
                                        {t("BOT_COUNTRY")}: {props.bot.country}
                                    </span>
                                </Space>
                                <div>
                                    <Space>
                                        <i className="fa-solid fa-wifi"/>
                                        <span>
                                            {props.bot.ip || "unknown"}
                                        </span>
                                    </Space>
                                </div>
                                <div>
                                    <Space>
                                        <i className="fa-solid fa-battery-half" style={{fontSize: 16}}/>
                                        <span>
                                            {props.bot.metadata.battery_level + "%"}
                                        </span>
                                    </Space>
                                </div>
                            </Space>

                            <div style={{display: "flex", justifyContent: "center", marginTop: 15, gridColumnGap: 15}}>
                                <NavLink to={"/filemanager?bot_id=" + props.bot.id}>
                                    <Button
                                        type="dashed"
                                        size="large"
                                        icon={(
                                            <span className="anticon">
                                                <i className="fa-regular fa-folder"/>
                                            </span>
                                        )}
                                    >
                                        {t("BOT_START_FTP_SESSION")}
                                    </Button>
                                </NavLink>
                            </div>
                        </div>

                        <div className="bot-item-row">
                            <Injections bot={props.bot} renderType={"cards"}/>
                        </div>
                    </div>
                </div>

                <Actions bot={props.bot}/>
            </div>

            {botData && (
                <BotInfo bot={botData} visible={showBotInfo} setVisible={setShowBotInfo}/>
            )}
        </Col>
    );
};

export default BotsItem;
