import { toast } from "react-toastify";
import DialogTitle from "@mui/material/DialogTitle";
import DialogContent from "@mui/material/DialogContent";
import DialogContentText from "@mui/material/DialogContentText";
import DialogActions from "@mui/material/DialogActions";
import { Button } from "@mui/material";
import Dialog from "@mui/material/Dialog";
import * as React from "react";

export const clickableLink=(link, label)=> {
    return <a href={link}>{label}</a>;
}

export function getDeletedRecordWithRefList(referenceList, updatedList) {
    return referenceList.filter(x => !updatedList.includes(x));
}

export function getIdList(input) {
    const output = input.map(function (obj) {
        return obj.id;
    });
    return output;
}

export function getObjectById(list, id) {
    const obj = list.find((element) => {
        return element.id === id;
    });
    return obj === undefined || Object.keys(obj).length <= 0 ? null : obj
}

export function getObjectByValue(list, valueName, value) {
    const obj = list.find((element) => {
        return element[valueName] === parseInt(value);
    });
    // console.log(obj);
    return obj === undefined || Object.keys(obj).length <= 0 ? null : obj
}

export function getObjectByType(list, valueName, value) {
    // console.log(list)
    // console.log(valueName)
    // console.log(value)
    const obj = list.find((element) => {
        // console.log(element[valueName])
        return element[valueName] === value;
    });
    // console.log(obj);
    return obj === undefined || Object.keys(obj).length <= 0 ? null : obj
}


export function removeObjectWithId(arr, id) {
    const arrCopy = Array.from(arr);
    const objWithIdIndex = arrCopy.findIndex((obj) => obj.id === id);
    arrCopy.splice(objWithIdIndex, 1);
    return arrCopy;
}

export function getComboValueByLabel(comboList, input) {
    for (let i = 0; i < comboList.length; i++) {
        if (comboList[i].label === input) {
            return comboList[i];
        }
    }
    return input === undefined ? null : input;
}

export function getDateString(queryDateArray) {
    return (
        queryDateArray[0]
        + "-" +
        queryDateArray[1]
        + "-" +
        queryDateArray[2]
        + " " +
        queryDateArray[3]
        + ":" +
        queryDateArray[4]
        + ":" +
        queryDateArray[5]
    )
}

const manualCloseToastOptions = {
    position: "bottom-right",
    autoClose: 30000,
    hideProgressBar: false,
    closeOnClick: true,
    pauseOnHover: true,
    draggable: true,
    progress: undefined,
    theme: "light",
};

export const notifySaveSuccess = () => {
    const userType = JSON.parse(localStorage.getItem("userData")).type;
    const msg = userType === "GLD" ? "Save success!" : "儲存成功!";
    toast.success(msg, manualCloseToastOptions);
};

export const notifyCreateSuccess = () => {
    const userType = JSON.parse(localStorage.getItem("userData")).type;
    const msg = userType === "GLD" ? "Create success!" : "創建成功!";
    toast.success(msg, manualCloseToastOptions);
};

export const notifyVerifySuccess = () => {
    const userType = JSON.parse(localStorage.getItem("userData")).type;
    const msg = userType === "GLD" ? "Verify success!" : "驗證成功!";
    toast.success(msg, manualCloseToastOptions);
};

export const notifyDeleteSuccess = () => {
    const userType = JSON.parse(localStorage.getItem("userData")).type;
    const msg = userType === "GLD" ? "Delete success!" : "刪除成功!";
    toast.success(msg, manualCloseToastOptions);
};

export const notifyLockSuccess = () => {
    toast.success("Lock success!", manualCloseToastOptions);
};

export const notifyUnlockSuccess = () => {
    toast.success("Unlock success!", manualCloseToastOptions);
};

export const notifyActiveSuccess = () => {
    toast.success("Active success!", manualCloseToastOptions);
};

export const notifyDownloadSuccess = () => {
    const userType = JSON.parse(localStorage.getItem("userData")).type;
    const msg = userType === "GLD" ? "Download success!" : "下載成功!";
    toast.success(msg, manualCloseToastOptions);
};

export const notifyActionSuccess = (actionMsg) => {
    toast.success(actionMsg ?? "Success", manualCloseToastOptions);
};

export const notifyActionError = (actionMsg) => {
    toast.error(`${actionMsg}`, manualCloseToastOptions);
};

export const notifyActionWarning = (actionMsg) => {
    toast.warn(`${actionMsg}`, manualCloseToastOptions);
}

export function prettyJson(json) {
    // console.log(json);
    // console.log(JSON.stringify(json, null, 2));
    return (
        <div>{JSON.stringify(json, null, 2)}</div>
    );
}

export function GeneralConfirmWindow({
    isWindowOpen,
    title,
    content,
    onNormalClose,
    onConfirmClose }) {
    return (
        <Dialog
            open={isWindowOpen}
            onClose={onNormalClose}
            aria-labelledby="alert-dialog-title"
            aria-describedby="alert-dialog-description"
        >
            <DialogTitle id="alert-dialog-title">
                {title}
            </DialogTitle>
            <DialogContent>
                <DialogContentText id="alert-dialog-description">
                    {content}
                </DialogContentText>
            </DialogContent>
            <DialogActions>
                <Button onClick={onNormalClose}>Cancel</Button>
                <Button onClick={onConfirmClose} autoFocus>
                    Confirm
                </Button>
            </DialogActions>
        </Dialog>
    )
}