var dgnsService = (function () {
    var surveyApiData = [];
    return {
        surveyApiData : surveyApiData,
    }
}())

dgnsService.dgnsApiModule = function () {
    var that = this;
    return {
        surveyApiRequest : function (option) {
            $.ajax({
                url: "/clfd/dgns/survey.json",
                type: "GET",
                success: function (res) {
                    if (res.result === "success") {
                        that.surveyData = res.survey;
                        if (option && option.scb) {
                            option.scb(res);
                        }
                    } else {
                        if (option && option.ecb) {
                            option.ecb(res);
                        }
                    }
                }
            });
        },
        surveyExamApiRequest: function (data, cbOption) {
            $.ajax({
                url: "/clfd/dgns/surveyExam.do",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                type: "POST",
                success: function (res) {
                    if (res.result === "success") {
                        console.log(res);
                        if (cbOption && cbOption.scb) {
                            cbOption.scb(res);
                        }
                    } else {
                        if (cbOption && cbOption.ecb) {
                            cbOption.ecb(res);
                        }
                    }
                },
                error : function (e) {
                    if (cbOption && cbOption.ecb) {
                        cbOption.ecb(e);
                    }
                }
            });
        },
        moveToDgnsResultPage: function (odScore, srScore, pnScore, wtScore) {
            var $form = document.createElement("form");
            $form.setAttribute("charset", "UTF-8");
            $form.setAttribute("method", "GET");
            $form.setAttribute("action", "/clfd/dgns/result.do");
            this.appendInput($form, "od", odScore);
            this.appendInput($form, "sr", srScore);
            this.appendInput($form, "pn", pnScore);
            this.appendInput($form, "wt", wtScore);
            document.body.appendChild($form);
            $form.submit();
        },
        appendInput : function ($form, inputName, inputValue) {
            var $input = document.createElement("input");
            $input.setAttribute("type", "hidden");
            $input.setAttribute("name", inputName);
            $input.setAttribute("value", inputValue);
            $form.appendChild($input);
        }
    }
}

dgnsService.dgnsViewModule = function () {
    var that = this;
    return {
        // rootDiv에 surveyApiData를 그리는 함수
        drawSurvey : function ($surveyRootDit, dgnsTyCdList, dgnsAskList) {
            if (!dgnsTyCdList || !dgnsAskList || dgnsTyCdList.length < 1 || dgnsAskList.length < 1) {
                alert("피부타입 테스트 설문이 없습니다.");
            }
            dgnsTyCdList = _.sortBy(dgnsTyCdList, ['cdDtlOrd']);
            var surveyRadioGroupArray = [];
            for (var i=0;i<dgnsTyCdList.length;i++) {
                this.drawDgnsTypeGroup($surveyRootDit, dgnsTyCdList[i], _.filter(dgnsAskList, {'dgnsTyCd': dgnsTyCdList[i].cdDtlId}), surveyRadioGroupArray);
            }
            return surveyRadioGroupArray;
        },
        drawDgnsTypeGroup: function ($surveyRootDiv, dgnsTyCd, dgnsAskList, surveyRadioGroupArray) {
            var stepText = "";
            var tyText = "";
            switch (dgnsTyCd.cdDtlId) {
                case "OD" : {
                    stepText = "QUESTION 01 (OILY VS. DRY)";
                    tyText = "오일지수 테스트 (4문항)";
                    break;
                }
                case "SR" : {
                    stepText = "QUESTION 02 (SENSITIVE VS. RESISTANT)";
                    tyText = "민감성 테스트 (6문항)";
                    break;
                }
                case "PN" : {
                    stepText = "QUESTION 03 (PIGMENTED VS. NON-PIGMENTED)";
                    tyText = "색소침착 테스트 (4문항)";
                    break;
                }
                case "WT" : {
                    stepText = "QUESTION 04 (WRINKLED VS. TIGHT)";
                    tyText = "피부탄력 테스트 (9문항)";
                    break;
                }
                default : break;
            }

            dgnsAskList = _.sortBy(dgnsAskList, ['dgnsAskOrd']);
            var $surveyTyDiv = $("<div class='dia-inner01'></div>");
            $surveyRootDiv.append($surveyTyDiv);
            var $surveyStepText = $("<div class='dia-inner01__step'>"+ stepText +"</div>");
            $surveyTyDiv.append($surveyStepText);
            var $surveyTyText = $("<div class='dia-inner01__desc'>"+ tyText +"</div>");
            $surveyTyDiv.append($surveyTyText);
            var $surveyProgBox = $("<div class='dia-inner01__prog'></div>");
            $surveyTyDiv.append($surveyProgBox);
            var $surveyProgImg = $("<div class='dia-inner01__prog__img__" + dgnsTyCd.cdDtlId.toString().toLowerCase() + "'></div>");
            $surveyProgBox.append($surveyProgImg);

            var $surveyAskDiv = $("<div class='dia-inner02'></div>");
            $surveyRootDiv.append($surveyAskDiv);

            // radio input의 name 정보를 가지고 있는 배열
            // var surveyRadioGroupArray = [];
            for (var j=0; j<dgnsAskList.length; j++) {
                var ask = dgnsAskList[j];
                var $surveyQuesDiv = $("<div class='dia-inner02__ques'></div>");
                var $surveyAskNum = $("<div class='dia-inner02__ques__num'>"+ ask.dgnsAskOrd +".</div>");
                $surveyQuesDiv.append($surveyAskNum);
                var $surveyAskText = $("<div class='dia-inner02__ques__content'>"+ ask.dgnsAskCont +"</div>");
                $surveyQuesDiv.append($surveyAskText);
                $surveyAskDiv.append($surveyQuesDiv);
                var $surveyBoxDiv = $("<div class='dia-inner02__box'></div>");
                $surveyAskDiv.append($surveyBoxDiv);

                var $surveyAnsForm = $("<form></form>");
                $surveyBoxDiv.append($surveyAnsForm);

                var inputName = dgnsTyCd.cdDtlId + "_" + ask.dgnsAskId;

                // surveyRadioGroupArray에 라디오 버튼 데이터 저장
                surveyRadioGroupArray.push({
                    dgnsTyCd: dgnsTyCd.cdDtlId,
                    dgnsTyNm : dgnsTyCd.cdDtlNm,
                    inputName: inputName,
                    dgnsAskId: ask.dgnsAskId,
                    dgnsAskOrd: ask.dgnsAskOrd,
                });

                var ansArray = ask.dgnsAnsList;
                ansArray = _.sortBy(ansArray, ['dgnsAnsOrd']);
                for (var k=0; k<ansArray.length; k++) {
                    var ans = ansArray[k];
                    var inputValue = inputName + "_" + ans.dgnsAnsId + "_" + ans.dgnsAnsOrd;

                    var $surveyAnsBox = $("<div class='dia-inner02__box__choice'>");
                    $surveyAnsForm.append($surveyAnsBox);

                    var $surveyAnsInput = $("<input style='cursor: pointer' class='dia-inner02__box__choice__num'/>");
                    $surveyAnsInput.attr("type", "radio");
                    $surveyAnsInput.attr("name", inputName);
                    $surveyAnsInput.attr("value", inputValue);
                    $surveyAnsInput.attr("id", inputValue);
                    $surveyAnsBox.append($surveyAnsInput);
                    // $surveyAnsForm.append(ans.dgnsAnsCont);

                    var $surveyAnsLabel = $("<label style='cursor: pointer' class='dia-inner02__box__choice__content'>"+ans.dgnsAnsCont+"</label>")
                    $surveyAnsLabel.attr("for", inputValue);
                    $surveyAnsBox.append($surveyAnsLabel);
                }
            }
            return surveyRadioGroupArray;
        },
        drawSurveyBtn : function ($surveyRootDiv) {
            var $submitBtnDiv = $("<div style='text-align: center; padding: 20px;'></div>");
            $surveyRootDiv.append($submitBtnDiv);
            var $submitSurveyBtn = $("<div id='submit_survey' class='dia-btn__submit'>" +
                "<span id='submitText'>제출하기</span>" +
                // "<div id='submitSpinner' class='spinner-border' style=''></div>" +
                "</div>");
            $submitBtnDiv.append($submitSurveyBtn);
            var $submitSpinner = $("<div id='submit_spinner' style='display: none' class='dia-btn__submit'>" +
                "피부타입을 도출 중..." +
                "</div>");
            $submitBtnDiv.append($submitSpinner);
            var $totalSelectBtn = $("<div id='total_select' class='dia-btn__submit' style='margin-top: 10px; display: none'>랜덤선택</div>");
            $submitBtnDiv.append($totalSelectBtn);

            return {
                $submitSurveyBtn: $submitSurveyBtn,
                $totalSelectBtn: $totalSelectBtn
            }
        }
    }
}