/* =================================
 * 결제버튼
 ================================= */
class PayBtn {
    constructor(data, billUid) {
        this.data = data;
        this.billUid = billUid;
    }

    create() {
        this.clz = this.getPayClz();
        this.paymentInfoApi = this.getPaymentInfoApi();
        this.gnrPaymentWindow();
        const payEvent = this.getPayBtnClickEvent();
        return this.gnrPayButton(payEvent).outerHTML;
    }

    getPayClz() {
        alert('Unimplemented "getPayClz" Function!!');
    }

    getPaymentInfoApi() {
        alert('Unimplemented "getPaymentInfoApi" Function!!');
    }

    pageBlocking() {
        this.blocking = true;
        const msg = ' 결제정보 요청 중 입니다<p/>잠시만 기다려 주세요';
        const img = 'data:image/gif;base64,R0lGODlhDwAPANUAAP////f3//f39+/39+/v9+bv7+bm797m5t7m797e5tbe5s7e5s7W5s7W3sXW3sXO3r3O3r3O1r3F1rXFzrXF1rW9zq29zqW1zqW1xZy1xZytxZStvZSlvYylvYyltYSctXuUrXOUrXOMrWuMrWuMpWuEpWOEpWOEnGN7nFp7nFJzlEpzlEprlEJrjDFahClSeyFKexBCaxBCcwAxY/4BAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBwA0ACwAAAAADwAPAAAGm0CAEEBAySKAAUcxbEZmMxUA9HoxhQSCEAYFqKoGoSASaYyhiipoSCCTC6rZ56UaABDCgsJBlsxaLgoFICsaYgQFDREvLA0YKSuRBXxuDROEkSsmEm4PCAYRGCIhGA8FQgcRDAIFEQ8aCiAiIiAXAgC3Ag8RChoSBh2zIgdDig8CGhoGAhK0Q2OqAMkXTc+7YRLJp9XVBRkRAU1BACH5BAUHADQALAAAAAAPAA8AAAaUQIAQIHBECMNkshCJKACWGQwxFBSEjSZANZtFhtmHQaCVSZPNpkIRYXQjA44EQDhkI1nRLJNxvVxJVg0TMSQtL4guAgoPaRMNKX4qKRsGjU0MDRkkJBICSQwRDgQKGg8iJSsrJBpPdAAGGhoEIh8dqqpJFxoZAB0iEhMpq0MFsnMbIi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FBiPjxIFMpUqEhENEQ8Kd0InMzARBaIRBhGkDFgAhwKxrQgFD7ERpwAOEQ4EpAICDKRDxBGmsQxCxsq4wLG/SwIEQq1KS0EAIfkEBQcANAAsAAAAAA8ADwAABphAmpAmcbkMgKQSMKR9Xq8NTbEiFYaICG3geqUAmtVKIgTAZrAI5kUDkFYpQXlGn2VaL4F4YpAgMzQydCQuEisdEiIiG0sMIjEpJCUfiiIfBQsPEZsTDSSKHTQPApubCg0OE4AES5oOAgo0Vw0aGhkSsQJXBkJyDAq1tUxNbZsEBRcaF0NXZQ80DkLNvdEEU9JNS0oCDwzEQQA7';
        const sty = "font-family: notokr-bold; font-size: 3rem; padding: 1rem 0rem;";
        const message = '<h1 style="' + sty + '"><img src="' + img + '" /> ' + msg + '</h1>';
        // const rootFontSize = window.getComputedStyle(document.body).getPropertyValue('font-size').replace(/[^0-9]/gi, '');
        const rootFontSize = $(window).width() >= 768 ? 10 : 5;
        const width = 50;
        const center = $(window).scrollLeft() + ($(window).width() / 2);
        const left = center - ((rootFontSize * width) / 2);
        const css = {
            position: 'fixed',
            width: width + 'rem',
            left: left + 'px',
            padding: '10px'
        }
        $.blockUI({message: message, css: css});
    }

    pageUnBlocking() {
        this.blocking = false;
        $.unblockUI();
    }

    gnrPaymentWindow() {
        let _instance = this;
        let _clz = this.clz;
        if (this.billUid == null || this.billUid == undefined || this.billUid == '') {
            _clz.getPaymentWindow = function () {
                alert('청구서 식별값이 없어 결제 진행이 불가 합니다.');
            }
        } else {
            _clz.getPaymentWindow = function () {
                _instance.pageBlocking();
            }
            $.get(this.paymentInfoApi, function (data) {
                _clz.getPaymentWindow = function () {
                    if (data.errCode !== 'OK') {
                        alert('[' + data.errCode + '] ' + data.errMsg);
                        return false;
                    }
                    _instance.paymentWindow(data);
                }
                if (_instance.blocking) {
                    _clz.getPaymentWindow();
                    _instance.pageUnBlocking();
                }
            });
        }
    }

    paymentWindow(data) {
        alert('Unimplemented "paymentWindow" Function!!');
    }

    getPayBtnClickEvent() {
        return 'javascript:' + this.clz.name + '.openPaymentWindow()';
    }

    gnrPayButton(hrefUrl) {
        let a = document.createElement('a');
        a.innerText = this.data.title.length > 4 ? this.data.title.substring(0, 4) + '..' : this.data.title;
        a.title = this.data.title;
        a.href = hrefUrl;

        let div = document.createElement('div');
        div.className = this.clz.name;
        div.innerHTML = a.outerHTML;

        return div;
    }

    static openPaymentWindow() {
        alert('Unimplemented "openPaymentWindow" Function!!');
    }
}

/* =================================
 * 카카오페이 결제버튼
 ================================= */
class KkoPayBtn extends PayBtn {

    constructor(data, billUid, payInfHost) {
        super(data, billUid);
        this.payInfHost = payInfHost;
    }

    getPayClz() {
        return KkoPayBtn;
    }

    getPaymentInfoApi() {
        return this.payInfHost + '/bill/pay/trg/kko/' + this.billUid + '/payinf';
    }

    paymentWindow(data) {
        location.href = data.resultInfo.url;
    }

    static openPaymentWindow() {
        KkoPayBtn.getPaymentWindow();
    }
}

/* =================================
 * 네이버페이 결제버튼
 ================================= */
class NvPayBtn extends PayBtn {

    constructor(data, billUid, payInfHost) {
        super(data, billUid);
        this.payInfHost = payInfHost;
    }

    getPayClz() {
        return NvPayBtn;
    }

    getPaymentInfoApi() {
        return this.payInfHost + '/bill/pay/trg/nv/' + this.billUid + '/payinf';
    }

    paymentWindow(data) {
        const oPay = Naver.Pay.create({
            "mode": data.resultInfo.mode,
            "payType": data.resultInfo.payType,
            "openType": data.resultInfo.openType, // 결제페이지 Open 타입
            "clientId": data.resultInfo.clientId, // clientId
        });
        oPay.open({
            "merchantUserKey": data.resultInfo.merchantUserKey,
            "merchantPayKey": data.resultInfo.merchantPayKey,
            "productName": data.resultInfo.payinf.productName,
            "totalPayAmount": data.resultInfo.payinf.totalPayAmount,
            "taxScopeAmount": data.resultInfo.payinf.taxScopeAmount,
            "taxExScopeAmount": data.resultInfo.payinf.taxExScopeAmount,
            "returnUrl": data.resultInfo.payinf.returnUrl + '?billUid=' + data.resultInfo.billUid,
            "productItems": data.resultInfo.payinf.productItems
        });
    }

    static openPaymentWindow() {
        NvPayBtn.getPaymentWindow();
    }
}