﻿/// <reference path="/Shared/Libs/jquery/3.4.1/jquery.js" />
/// <reference path="/Shared/Script/SharedCommon.js" />
/// <reference path="/Shared/Script/SharedVex.js" />
/// <reference path="/Shared/Script/Ajax.js" />
/// <reference path="/Shared/Libs/tooltipster_4.2.6/tooltipster.bundle.js" />


$(function () {
    VehicleSearchInit();
});


function VehicleSearchInit() {

    $("body").on("click", "#VinSearchButton", VinSearchButtonClick);
    $("body").on("keypress", "#VinSearchValue", function (ev) {
        return CallOnEnterKeyCode(ev, VinSearchButtonClick, true);
    });
    $("body").on("click", "#MobileVinSearchButton", VinSearchButtonClick);
    $("body").on("click", "#VehicleSearchOuter .VinInfoContainer", VinInfoContainerClick);

    $("body").on("click", ".ChooseOtherVehicleButton", ChooseOtherVehicleClick);

    $("body").on("click", ".MoreVehicleInfoButton", MoreVehicleInfoButtonClick);
    $("body").on("click", "#RequestVehicleInfoButton", RequestVehicleInfoButtonClick);

    $("body").on("click", ".ChassiSearchButton", ChassiSearchButtonClick);

    $("body").on("click", "#FluidAmountDetails", FluidAmountDetailsClick);

    InitInvalidVinToolTip();

    LoadFluidAmount();
}


function InitInvalidVinToolTip() {

    $(".VinSearchValueContainer").tooltipster({
        theme: "tooltipster-shadow", //"tooltipster-borderless"
        animation: "fade",
        position: ["right", "bottom"],
        autoClose: true,
        interactive: true,
        trigger: "custom",
        contentCloning: true,
        content: $("<div class='VinInvalid'>Ugyldig eller ukjent registreringsnummer</div>")
    });
}



function VinSearchButtonClick() {
    var data = {};
    data.vin = $(".VinSearchValue:visible").val();

    var country = $(".VinSearchValue:visible").data("country");

    if (!IsVin(data.vin, country)) {
        InvalidVin("Ugyldig regnr");
        return;
    }


    AjaxCall("/Shared/CommonAjax.aspx/VinSearch", data, VinSearchComplete, HandleAjaxErrorVex, $("#VinSeachButton"));
}


function VinInfoContainerClick() {

    var message = $("#VinInfoDetailsOuter").html();

    AlertVex(message, "StandardNice", Noop);
}


function VinSearchComplete(msg) {

    if (IsMsgNull(msg)) {
        InvalidVin("");
        return;
    }

    if (!msg.d.ok) {
        InvalidVin(msg.d.message);
        return;
    }

    var mobile = $(".VinAndBannerComponentMobileCheck:visible").length > 0;
    var attr = "data-redirect-" + (mobile ? "mobile" : "desktop");
    var url = $(".VinAndBannerComponent").attr(attr);

    if (!IsNullOrUndefined(url) && url === "noredirect") {
        AjaxCall("/Shared/CommonAjax.aspx/RenderVehicleSearchControl",
            {},
            function(msg) {
                if (IsMsgNull(msg)) {
                    return;
                }

                $(".VinAndBannerComponent").html(msg.d);
            },
            HandleAjaxErrorVex,
            $("#VinSeachButton"));

        return;
    }

    if (!IsNullOrUndefined(url)) {
        window.location = url;
        return;
    }

    url = $("#VehicleSearchOuterContainer").attr("data-href");

    if (IsNullOrUndefined(url)) {
        window.location = window.location;
        return;
    }

    window.location = url;

}

function InvalidVin(message) {
    ErrorShake($(".VinSearchValueContainer"));

    if (message === undefined || message === "") {
        message = "Ugyldig eller ukjent registreringsnummer (js)";
    }

    message = $("<div class='VinInvalid'>" + message + "</div>");

    $(".VinSearchValueContainer").tooltipster("content", message);
    $(".VinSearchValueContainer").tooltipster("open");


    setTimeout(function () {
        $(".VinSearchValueContainer").tooltipster("close");
    }, 8000);

}


function ChooseOtherVehicleClick() {
    AjaxCall("/Shared/CommonAjax.aspx/VinClear", {}, function () {
        window.location.reload();
    }, HandleAjaxErrorVex, null, true);
}


function MoreVehicleInfoButtonClick() {
    var data = {};

    AjaxCall("/Shared/CommonAjax.aspx/GetMoreVehicleInfo", data, function (msg) {
        AlertVex(msg.d, "Wide", Noop);
    }, HandleAjaxErrorVex);

}


function RequestVehicleInfoButtonClick() {
    var data = {};

    AjaxCall("/Shared/CommonAjax.aspx/RetryMoreVehicleInfo", data, function (msg) {
        $("#NoVehicleInfoFoundContainer").hide();
        $("#OuterRenderProperties").show();
        $("#OuterRenderProperties .Inner").html(msg.d);

    }, HandleAjaxErrorVex, $("#RequestVehicleInfoButton"));
}




function ChassiSearchButtonClick() {

    $("#ChassiSearchResult").text("");

    var data = {};
    data.chassi = $(".ChassiSearchValue").val();
    AjaxCall("/Shared/CommonAjax.aspx/ChassiSearch", data, ChassiSearchComplete, HandleAjaxErrorVex, $(this));
}


function ChassiSearchComplete(msg) {

    if (IsMsgNull(msg)) {
        ChassiSearchFailed("Ugyldig eller ukjent chassinummer (js)");
        return;
    }

    if (msg.d.markup !== "") {
        AlertVex(msg.d.markup, "WideNice Dark");
        return;
    }

    if (!msg.d.ok) {
        ChassiSearchFailed(msg.d.message);
        return;
    }

    var url = $(".VehicleSearchByChassi").attr("data-redirect");

    if (!IsNullOrUndefined(url)) {
        window.location = url;
        return;
    }

    window.location = "/";
}

function ChassiSearchFailed(result) {

    $("#ChassiSearchResult").text(result);

    setTimeout(function () {
        $("#ChassiSearchResult").text("");
    }, 8000);


    ErrorShake($(".ChassiSearchValue"));
}


function LoadFluidAmount() {
    if ($("#VinOuterContainer #FluidAmountDetails").length === 0) {
        return;
    }
    console.log("running fluids");

    var data = {};

    AjaxCall("/Shared/CommonAjax.aspx/LoadFluidAmount", data, LoadFluidAmountComplete, HandleAjaxErrorVex);
}

function LoadFluidAmountComplete(msg) {
    if (IsMsgNull(msg)) {
        return;
    }

    if (msg.d.Count === 0) {
        return;
    }

    console.log(msg.d);

    $("#FluidAmountDetails").show();
}


function FluidAmountDetailsClick() {

    var data = {};

    AjaxCall("/Shared/CommonAjax.aspx/GetFluidAmount", data, GetFluidAmountComplete, HandleAjaxErrorVex);

}

function GetFluidAmountComplete(msg) {
    var text = "Væskemengde kunne ikke vises.";
    var style = "StandardNice";

    if (msg != null && msg.d != null) {
        text = msg.d.Content;
        style = msg.d.Style;
    }

    AlertVex(text, style);
}
