var prodDtlService = (function () {
    var prod = {
        prodId : undefined,
        rcmdNo : undefined,
        like : undefined,
    };
    return {
        setProd: function (_prod) {
            prod.prodId = _prod.prodId;
            prod.rcmdNo = _.toNumber(_prod.rcmdNo);
            prod.like = _.isBoolean(_prod.like) ? _prod.like : _prod.like === "true"
        },
        getProd: function () {
            return _.clone(prod);
        },
        toggleLike: function () {
            prod.like = !prod.like;
            if (prod.like) {
                prod.rcmdNo++;
            } else {
                prod.rcmdNo--;
            }
        },
    }
}());

prodDtlService.viewModule = function () {
    var that = this;

    function appendProdToRootDiv($root, prod) {
        var $card = $("<div class=\"dtl-inner02__rec__list__content\"></div>");
        var $anchor = $("<a href='/clfd/prod/dtl.do?prod=" + prod.prodId + "'></a>");
        var $img = $("<img style=\"margin: auto auto;\" src='/images/prod/" + prod.prodImage + "' alt=\"\">");
        var $prodDiv = $("<div class=\"card__contents\"></div>");
        var $prodNm = $("<div class=\"dtl-text__pr__rec\">" + prod.prodNm + "</div>");
        var $prodDtlDiv = $("<div class=\"card__contents__ac\"></div>");
        var $price = $("<div class=\"dtl-text__am__rec\">" + clfdUtils.formattingDot(prod.prodPrc) + "</div>");
        var $capacity = $("<div class=\"dtl-text__ca__rec\"> / " + prod.prodCpcty + "ml</div>");

        $prodDtlDiv.append($price);
        $prodDtlDiv.append($capacity);
        $prodDiv.append($prodNm);
        $prodDiv.append($prodDtlDiv);
        $anchor.append($img);
        $anchor.append($prodDiv);
        $card.append($anchor);
        $root.append($card);
    }

    return {
        drawLikeView: function ($likeRoot) {
            $likeRoot.empty();
            var prod = that.getProd();
            if (prod.like) {
                $likeButton = $('<i class="fas fa-heart active">' + prod.rcmdNo + '</i>');
            } else {
                $likeButton = $('<i class="fas fa-heart">' + prod.rcmdNo + '</i>');
            }
            $likeRoot.append($likeButton)
        },
        drawSkTyProdList: function ($root, skTy, prodId) {
            clfdCommonService.prodListApiRequest(
                {
                    scb: function (res) {
                        if (!res.prodList) return;
                        for (var i = 0; i < res.prodList.length; i++) {
                            // 같은 제품이 추천되는 경우를 제외
                            if (res.prodList[i].prodId != prodId && i < 4) {
                                appendProdToRootDiv($root, res.prodList[i]);
                            }
                        }
                    },
                    ecb: function (res) {
                        console.log(res);
                    }
                },
                {
                    start: 0,
                    length: 5,
                    pagingYn: true,
                    prodNmLike: null,
                    prodTyArray: null,
                    skTyArray: [skTy],
                    sortOrd: 'DESC',
                    sort: 'RCMD_NO'
                }
            );
        }
    }
}
