﻿var BOOLEAN_TYPE = 0;
var COND_DONTKNOW = "0";
var COND_YES = "1";
var COND_NO = "2";

$.showConditionQuestions = function () {
    $.toggleOverylay({ lock: false }); //showing overlay
    var $conditionWrapper = $('<div id="conditionWrapper" />').prependTo('body').hide();
    $conditionWrapper.append('<h3>' + $.tr('Mer information behövs') + '</h3>');
    $conditionWrapper.append('<div class="header">' + $.tr('För att enbart delar som passar just din bil ska kunna visas behöver du besvara följande frågor.') + '</div>');
    for (var idx in globalQuestions) {
        $.addConditionQuestion($conditionWrapper, globalQuestions[idx]);
    }
    $conditionWrapper.append('<div class="footer"><input type="button" id="conditionAnswerBtn" value="' + $.tr('Svara') + '" /></div>');
    $conditionWrapper.css('top', $(document).scrollTop() + 200).css({'padding-top': '30px', 'cursor': 'move'}).show().draggable();
    $('#conditionAnswerBtn').click(function () {
        $.sendAnswers();
    });
}

$.addConditionQuestion = function (wrapper, question) {
    if (question.Type == BOOLEAN_TYPE) {
        $.addBooleanQuestion(wrapper, question);
    }
    else {
        $.addMultipleChoiceQuestion(wrapper, question);
    }
}

$.addBooleanQuestion = function (wrapper, question) {
    var questionWrapper = $('<div class="questionWrapper" />');
    questionWrapper.append('<div class="question">' + question.Choices[0].Text + '</div>');
    var choiceList = $('<ul class="answers boolean" id="answers' + question.Choices[0].Code + '" />');
    $.appendChoiceItem(choiceList, question.Choices[0].Code, COND_YES, $.tr('Ja'), false);
    $.appendChoiceItem(choiceList, question.Choices[0].Code, COND_NO, $.tr('Nej'), false);
    $.appendChoiceItem(choiceList, question.Choices[0].Code, COND_DONTKNOW, $.tr('Vet ej'), true);
    questionWrapper
        .append(choiceList)
        .append('<div class="clear">&nbsp;</div>');
    wrapper.append(questionWrapper);
}

$.addMultipleChoiceQuestion = function (wrapper, question) {
    var questionWrapper = $('<div class="questionWrapper" />');
    questionWrapper.append('<div class="question">' + $.tr('Välj ett av följande alternativ') + '</div>');
    var choiceList = $('<ul class="answers mc" />');
    for (var i in question.Choices) {
        $.appendChoiceItem(choiceList, question.Choices[0].Code, question.Choices[i].Code, question.Choices[i].Text, false);
    }
    $.appendChoiceItem(choiceList, question.Choices[0].Code, COND_NO, $.tr('Inget av ovanstående'), false);
    $.appendChoiceItem(choiceList, question.Choices[0].Code, COND_DONTKNOW, $.tr('Vet ej'), true);
    questionWrapper.append(choiceList);
    questionWrapper.append('<div class="clear">&nbsp;</div>');
    wrapper.append(questionWrapper);
}

$.appendChoiceItem = function (choiceList, name, value, text, checked) {
    var checkedString = (checked) ? "checked = checked" : "";
    var idString = name + value;
    var item = $('<li />')
        .append('<input type="radio" id="' + idString + '" name="' + name + '" value="' + value + '" ' + checkedString + ' />')
        .append('<label for="' + idString + '">' + text + '</label>');
    choiceList.append(item);
}

$.sendAnswers = function () {
    var answers = new Array();
    $.parseBooleanAnswers(answers);
    $.parseMultipleChoiceAnswers(answers);
    $('#conditionWrapper').remove();
    $.toggleOverylay({ lock: false });
    $.submitResponses(answers);
}

$.parseBooleanAnswers = function (answers) {
    $('#conditionWrapper .boolean input:checked').each(function () {
        answers.push(this.name);
        answers.push($(this).val());
    });
}

$.parseMultipleChoiceAnswers = function (answers) {
    $('#conditionWrapper ul.mc').each(function () {
        var checkedAnswer = $(this).find("input:checked");
        switch (checkedAnswer.val()) {
            case COND_DONTKNOW:
                $.addAllChoicesWithValue(answers, this, COND_DONTKNOW);
                break;
            case COND_NO:
                $.addAllChoicesWithValue(answers, this, COND_NO);
                break;
            default:
                $.answerNoToAllExceptSelected(answers, this, checkedAnswer.val());
                break;
        }
    });
}

$.addAllChoicesWithValue = function (answers, choiceList, setValue, skipThisValue) {
    $(choiceList).find("input").each(function () {
        var checkBoxValue = $(this).val();
        if (checkBoxValue != COND_DONTKNOW && checkBoxValue != COND_NO) {
            answers.push(checkBoxValue);
            answers.push(setValue);
        }
    });
}

$.answerNoToAllExceptSelected = function (answers, choiceList, selectedValue) {
    $(choiceList).find("input").each(function () {
        var checkBoxValue = $(this).val();
        if (checkBoxValue == selectedValue) {
            answers.push(checkBoxValue);
            answers.push(COND_YES);
        }
        else if (checkBoxValue != COND_DONTKNOW && checkBoxValue != COND_NO) {
            answers.push(checkBoxValue);
            answers.push(COND_NO);
        }
    });
}

$.submitResponses = function submitResponses(answers) {
    var jsonAnswers = JSON.stringify(answers);
    $.clearFilterInput();
    $.ajaxLoadingStatus({ text: $.tr('Hämtar sökresultat, var god vänta.') });
    $.ajax({
        type: "GET",
        contentType: "application/json",
        url: "/webservices/productservice.svc/AnswerConditions?answers=" + jsonAnswers,
        dataType: "json",
        processData: false,
        success: function (p) {
            if ($.ajaxErrorHandler(p)) {
                $.globalProductList(p.d);
                updatePager();
                $.ajaxLoadingStatus({ create: false, update: false });
            }
            p = undefined;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $.showErrorMessage($.tr('Någonting gick fel när sidan försökte hämta produkter via artikelgrupp'));
        }
    });
}
