

import React, { useState, useEffect } from "react";

import { Modal, Col, Skeleton, Alert, Typography } from 'antd';

import { Dashboard } from './../../../../models/'

import { PopQueryDashboard } from './../../../'

import { StatisticCard, BasicTable, BarGraph } from 'soxo-firebase-core';

const { Title } = Typography;

/**
 * Dashboard Card
 *
 * @param {*} param0
 */
export default function DashboardCard({ record }) {

    const [visible, setVisible] = useState(false);

    const [loading, setLoading] = useState(true);

    const [result, setResult] = useState({});

    useEffect(() => {
        executeQuery(record.id);
    }, []);

    function executeQuery(id) {
        setLoading(true);

        // Execute dashboard
        Dashboard.execDashboard(id)
            .then((response) => {
                var data = {};

                let reportData = {};

                // @todo 
                // We are in the transition of incorporating the api bootstrap core for nura and 
                // other projects , so we are handling both here . 
                // When the reporting module is build , we have to consider its changes . 

                if (response.result) {

                    reportData = response.result[0];

                } else {

                    if (response[0]) {

                        reportData = response[0];
                    }
                }

                setResult(reportData);

                setLoading(false);
            })
            .catch(() => {
                setResult({ error: 'Error' });
            });
    }

    /**
     *
     */
    function viewPopQueryModal(id) {
        setVisible(true);
    }

    /**
     *
     */
    function executePopQuery() { }

    var props = {};

    if (loading) {
        props.loading = true;
    }

    /**
     * Display Each card according to graph type
     */
    function displayDashboardTile(record) {
        // console.log(result);

        switch (record.tiletypeptr) {
            case 'BASIC':
                return <StatisticCard result={result[0]} {...props} />;

            case 'TABLE':
                return <BasicTable result={result} {...props} />;

            case 'LINEGPH':
                return <BarGraph data={result} {...props} />;

            default:
                return '';
        }
    }

    let span = 6;

    switch (record.tiletypeptr) {
        case 'BASIC':
            break;

        case 'TABLE':
            span = 24;
            break;

        case 'LINEGPH':
            span = 24;
            break;

        default:
        // return '';
    }

    function onCancel() {
        setVisible(false);
    }

    return (
        <Col className="dashboard-card card" span={span} lg={{ span: span }} xs={{ span: 24 }} key={record.id}>
            <Title level={5}>{record.text}</Title>

            {result.error ? (
                <>
                    <Alert message="Error loading values" type="error" showIcon />
                </>
            ) : (
                <>{loading ? <Skeleton /> : displayDashboardTile(record)}</>
            )}

            <span className="link" onClick={viewPopQueryModal}>View</span>

            <Modal
                width="80%"
                destroyOnClose={true}
                visible={visible}
                title={record.text}
                footer={null}
                okText="Create"
                cancelText="Cancel"
                onCancel={onCancel}
                onOk={() => { }}
            >
                <PopQueryDashboard record={record} id={record.id} />
            </Modal>
        </Col>
    );
}
