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

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
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 kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

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

public class NoticeWebViewActivity extends BaseActivity {
    private WebView mWebView;

    private String mUrl;
    private int mNo;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_notice_webview;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return null;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.BOTTOM;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mUrl = getIntent().getStringExtra(HFActivityUtil.INTENT_NOTICE_URL);
        mNo = getIntent().getIntExtra(HFActivityUtil.INTENT_NOTICE_NO, 0);


        findViewById(R.id.doNotLookAgain_textView).setOnClickListener(onClickListener);
        findViewById(R.id.positive_textView).setOnClickListener(onClickListener);

        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setTextZoom(100);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
//        mWebView.setInitialScale(50);
//        mWebView.getSettings().setDefaultFontSize(GWCommonUtil.getPixelFromDimension(this, R.dimen.dimen_dp30));

        mWebView.setWebViewClient(new CommonWebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        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() {
                    //...
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack())
            mWebView.goBack();
        else
            super.onBackPressed();
    }

    /**************************************************
     * 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;

            showLoadingDialog(false);

            if (TextUtils.isEmpty(url) == false && url.endsWith(".pdf")) {
                try {
                    HFActivityUtil.startUrl(NoticeWebViewActivity.this, url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @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);
        }
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.doNotLookAgain_textView:
                    HFInfoUtil.setNoticeNo(NoticeWebViewActivity.this, mNo);
                    finish();
                    break;
                case R.id.positive_textView:
                    finish();
                    break;
            }
        }
    };
}
