package com.dbs.mplus.fatima.framework.util;

import android.content.Context;
import android.os.Build;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * 웹 에러화면 표시 처리
 *
 *
 */
public class MPlusWebErrorUtil {
    /**
     * 만들어진 문자열을 웹뷰에 표현하기
     * @param view
     * @param data
     */
    public static void loadStringData(WebView view, String data) {
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            view.loadData(data, "text/html", "UTF-8");
        }
        else {
            view.loadData(data, "text/html; charset=UTF-8", null);
        }
    }

    /**
     * 에러 메시지 박스
     * @param errorMsg 표시될 에러 메시지
     * @return
     */
    public static String getMessageBox(String errorMsg) {
        final StringBuilder builder = new StringBuilder();

        // 에러 메시지가 표현될 박스
        builder.append("<p style='position: relative; padding: 1em 1em 1em 1em;background-color: #FFF; ");
        builder.append(" border-radius: 8px; box-shadow: 2px 2px 3px rgba(0,0,0,0.2); font-size: 1.2em; ");
        builder.append(" letter-spacing: -1px; font-weight: bold; text-align: center; line-height: 1.3em; margin: 1em 0;'>");
        if(errorMsg != null) {
            builder.append(errorMsg);
        }

        builder.append("</p>");

        return builder.toString();
    }

    /**
     * 돌아가기 링크 생성용
     * @param url
     * @param name
     * @return
     */
    public static String getReturnURL(String url, String name) {
        final StringBuilder builder = new StringBuilder();

        // 링크만들기
        builder.append("<a href=\"").append(url).append("\" style=\"");
        builder.append(" background-color: #5fb63b; border-color: #1f1f1f; color: #fff; border-width: 0; ");
        builder.append(" border-radius: 0.325em; font-size: 16px; margin: .5em 0; padding: .7em 1em; ");
        builder.append(" display: block; position: relative; text-align: center; text-overflow: ellipsis; ");
        builder.append(" overflow: hidden; white-space: nowrap; cursor: pointer; ");
        builder.append(" text-decoration: none!important; border-style: solid; line-height: 1.3; ");
        builder.append("\">");
        builder.append(name).append("</a>");

        return builder.toString();
    }

    /**
     * 에러 메시지 변환
     * @param context
     * @param errorCode
     * @return
     */
    public static String getErrorString(Context context, int errorCode) {
        String errorStr = "";

        switch(errorCode) {
            case WebViewClient.ERROR_AUTHENTICATION :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_authentication");
                break;

            case WebViewClient.ERROR_BAD_URL :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_bad_url");
                break;

            case WebViewClient.ERROR_CONNECT :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_connect");
                break;

            case WebViewClient.ERROR_FAILED_SSL_HANDSHAKE :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_failed_ssl_handshake");
                break;

            case WebViewClient.ERROR_FILE :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_file");
                break;

            case WebViewClient.ERROR_FILE_NOT_FOUND :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_file_not_found");
                break;

            case WebViewClient.ERROR_HOST_LOOKUP :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_host_lookup");
                break;

            case WebViewClient.ERROR_IO :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_io");
                break;

            case WebViewClient.ERROR_PROXY_AUTHENTICATION :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_proxy_authentication");
                break;

            case WebViewClient.ERROR_REDIRECT_LOOP :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_redirect_loop");
                break;

            case WebViewClient.ERROR_TIMEOUT :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_timeout");
                break;

            case WebViewClient.ERROR_TOO_MANY_REQUESTS :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_too_many_requests");
                break;

            case WebViewClient.ERROR_UNKNOWN :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_unknown");
                break;

            case WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_unsupported_auth_scheme");
                break;

            case WebViewClient.ERROR_UNSUPPORTED_SCHEME :
                errorStr = MPlusResourceUtil.getStringByName(context, "mf_error_webviewclient_unsupported_scheme");
                break;
        }

        return errorStr;
    }
}
