package com.epson.mobilephone.common.guide;

import android.content.Context;
import android.os.Bundle;
import android.support.p000v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;

import com.epson.guidelib.R;

public class GuideWebviewFragment extends Fragment {
    private static final String ARG_PARAM1 = "html_file_path";
    MyClickListener mClickListerner;
    private String mHtmlPath;
    private WebView mWebView;
    private FrameLayout mWebViewFrame;

    public interface MyClickListener {
        void onClickClose();
    }

    public static GuideWebviewFragment newInstance(String str) {
        GuideWebviewFragment guideWebviewFragment = new GuideWebviewFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_PARAM1, str);
        setArguments(bundle);
        return guideWebviewFragment;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (getArguments() != null) {
            mHtmlPath = getArguments().getString(ARG_PARAM1);
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_guide_webview, viewGroup, false);
        mWebView = (WebView) inflate.findViewById(R.id.guideWebview);
        mWebViewFrame = (FrameLayout) inflate.findViewById(R.id.guideWebViewFrame);
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mWebView.getLayoutParams();
        ViewGroup.MarginLayoutParams marginLayoutParams2 = (ViewGroup.MarginLayoutParams) mWebViewFrame.getLayoutParams();
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.setInitialScale(1);
        mWebView.loadUrl(mHtmlPath);
        int webViewDP = Utils.getWebViewDP(getActivity(), getContext());
        marginLayoutParams.height = Utils.dptopx(webViewDP, getContext());
        marginLayoutParams.width = Utils.dptopx(webViewDP, getContext());
        marginLayoutParams2.height = Utils.dptopx(Utils.LAYOUT_MEARGIN + webViewDP, getContext());
        marginLayoutParams2.width = Utils.dptopx(webViewDP + Utils.LAYOUT_MEARGIN, getContext());
        mWebView.setLayoutParams(marginLayoutParams);
        mWebViewFrame.setLayoutParams(marginLayoutParams2);
        inflate.findViewById(R.id.closeImage).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mClickListerner != null) {
                    mClickListerner.onClickClose();
                }
            }
        });
        return inflate;
    }

    public void onPause() {
        super.onPause();
        mWebView.onPause();
    }

    public void onResume() {
        mWebView.onResume();
        super.onResume();
    }

    public void onDestroy() {
        WebView webView = mWebView;
        if (webView != null) {
            webView.destroy();
        }
        mWebView = null;
        super.onDestroy();
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mClickListerner = (MyClickListener) context;
        } catch (ClassCastException unused) {
            throw new ClassCastException(getActivity().toString() + "must implement OnArticleSelectedListener.");
        }
    }

    public void onDetach() {
        super.onDetach();
        mClickListerner = null;
    }
}
