package kr.co.hanwha.hifive.screen.main.webview;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseFragment;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;

/**
 * Created by sanghoonkwon on 2018. 2. 5..
 */

public class MainWebviewFragment extends BaseFragment {
    private LinearLayout mLayoutBottom;
    private ImageButton mImageButtonPre;
    private ImageButton mImageButtonNext;

    private WebView mWebView;

    private String mUrl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View parent = inflater.inflate(R.layout.fragment_main_webview, container, false);

        mLayoutBottom = (LinearLayout) parent.findViewById(R.id.bottom_linearLayout);
        mImageButtonPre = (ImageButton) parent.findViewById(R.id.pre_imageButton);
        mImageButtonNext = (ImageButton) parent.findViewById(R.id.next_imageButton);

        parent.findViewById(R.id.pre_imageButton).setOnClickListener(onClickListener);
        parent.findViewById(R.id.next_imageButton).setOnClickListener(onClickListener);

        mWebView = (WebView) parent.findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new CommonWebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.addJavascriptInterface(new HFJavaScript(), "Android");

        if (TextUtils.isEmpty(mUrl) == false) {
            if (mUrl.toLowerCase().startsWith("http") == false) mUrl = "http://" + mUrl;
            mWebView.loadUrl(mUrl);
        } else {
            showAlert(getString(R.string.alert_default_title), getString(R.string.alert_default_message), new HFSingleDialog.OnDialogCallback() {
                @Override
                public void onPositive() {
                    //...
                }
            });
        }
        return parent;
    }

    /**************************************************
     * Support methods
     **************************************************/
    public void setUrl(@NonNull String url) {
        mUrl = url;
    }

    private void checkBottomLayout() {
        if (mWebView.canGoBack() || mWebView.canGoForward()) {
            mLayoutBottom.setVisibility(View.VISIBLE);
            mImageButtonPre.setEnabled(mWebView.canGoBack());
            mImageButtonPre.setAlpha(mWebView.canGoBack() ? 1.0f : 0.3f);
            mImageButtonNext.setEnabled(mWebView.canGoForward());
            mImageButtonNext.setAlpha(mWebView.canGoForward() ? 1.0f : 0.3f);
        } else {
            mLayoutBottom.setVisibility(View.GONE);
        }
    }

    /**************************************************
     * WebViewClient
     **************************************************/
    public class CommonWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            showLoadingDialog(true);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            mUrl = url;//TODO- CHECK

            showLoadingDialog(false);
            checkBottomLayout();
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            showLoadingDialog(false);
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            showLoadingDialog(false);
        }
    }

    /**************************************************
     * WebViewClient
     **************************************************/
    public class HFJavaScript {
        public HFJavaScript() {
        }

        @JavascriptInterface
        public void openCustomPopup(String title, String message) {
            showAlert(title, message, new HFSingleDialog.OnDialogCallback() {
                @Override
                public void onPositive() {
                    //...
                }
            });
        }

        @JavascriptInterface
        public void closeWebview() {
            showToast("closeWebview()");

        }
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.pre_imageButton:
                    if (mWebView.canGoBack()) mWebView.goBack();
                    break;
                case R.id.next_imageButton:
                    if (mWebView.canGoForward()) mWebView.goForward();
                    break;
            }
        }
    };
}
