import Swal from "sweetalert2/dist/sweetalert2.js";
import "sweetalert2/src/sweetalert2.scss";
import $, { ajax } from "jquery";

const Toast = Swal.mixin({
    toast: true,
    position: "top-end",
    showConfirmButton: false,
    timer: 3000,
});

export default Toast;

export function toastAlert(action) {
    const swalWithBootstrapButtons = Swal.mixin({
        customClass: {
            confirmButton: "btn btn-success",
            cancelButton: "btn btn-danger",
        },
        buttonsStyling: false,
    });
    swalWithBootstrapButtons
        .fire({
            title: "Are you sure?",
            text: "You won't be able to revert this!",
            icon: "warning",
            showCancelButton: true,
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel!",
            cancelButtonColor: "#FF0000.",
            reverseButtons: true,
        })
        .then((result) => {
            if (result.isConfirmed) {
                const formData = new FormData();
                formData.append("_method", "DELETE");
                formData.append("_token", $('meta[name="csrf-token"]').attr("content"));
                ajax({
                    url: action,
                    method: "POST",
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        toastSuccess(data?.message);

                        if (data.redirect) {
                            location.href = data.redirect;
                            return;
                        }

                        (data.reload ?? true) && location.reload();
                    },
                    error: function (data) {
                        const error = data.responseJSON.message;
                        if (error) {
                            toastError(error);
                        }
                    },
                });
            }
        });
}

export function toastConfirm(config) {
    const swalWithBootstrapButtons = Swal.mixin({
        customClass: {
            confirmButton: "btn btn-success",
            cancelButton: "btn btn-danger",
        },
        buttonsStyling: false,
    });
    swalWithBootstrapButtons
        .fire({
            title: config.title || "Are you sure?",
            text: config.text || "To perform this action please confirm",
            icon: "warning",
            showCancelButton: true,
            confirmButtonText: "Yes, confirm!",
            cancelButtonText: "No, cancel!",
            cancelButtonColor: "#FF0000.",
            reverseButtons: true,
        })
        .then((result) => {
            if (result.isConfirmed) {
                if (config.form) {
                    config.form.submit();
                    return;
                }

                if (config.action && config.method === "REDIRECT") {
                    location.href = config.action;
                    return;
                }

                const formData = new FormData();
                formData.append("_method", config.method || "POST");
                formData.append("_token", $('meta[name="csrf-token"]').attr("content"));
                ajax({
                    url: config.action,
                    method: config.method == "GET" ? "GET" : "POST",
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        toastSuccess(data?.message);
                        data?.reload && location.reload();
                    },
                    error: function (data) {
                        const message = data.responseJSON.message;
                        if (message) {
                            toastError(message);
                        }
                    },
                });
            }
        });
}

export function toastSuccess(title = "Success") {
    Toast.fire({
        icon: "success",
        title,
        padding: "10px 20px",
    });
}

export function toastError(title = "Error") {
    Toast.fire({
        icon: "error",
        title,
        padding: "10px 20px",
    });
}

export function toastWarning(title = "Warning") {
    Toast.fire({
        icon: "warning",
        title,
        padding: "10px 20px",
    });
}

$(function () {
    const toastMessage = $("#session-toast").data("message");
    const type = $("#session-toast").data("type");
    if (toastMessage) {
        Toast.fire({
            icon: type,
            title: toastMessage,
            padding: "10px 20px",
        });
    }
});
