package cc.winboll.studio.webpagesources.fragment;

/**
 * @Author ZhanGSKen@QQ.COM
 * @Date 2023/07/04 13:45:32
 * @Describe 网页浏览视图 Fragment
 */
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import androidx.fragment.app.Fragment;
import cc.winboll.studio.libaes.views.AButton;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.webpagesources.R;
import cc.winboll.studio.webpagesources.common.BaseWebView;
import cc.winboll.studio.webpagesources.view.URLAddressView;
import com.baoyz.widget.PullRefreshLayout;

public class WebFragment extends Fragment {

    public static final String TAG = "WebFragment";

    View mView;
    BaseWebView mBaseWebView;
    URLAddressView mURLAddressView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        mView = inflater.inflate(R.layout.fragment_web, container, false);

        mURLAddressView = mView.findViewById(R.id.fragmentwebURLAddressView1);
        mURLAddressView.initAnchorView((LinearLayout)mView.findViewById(R.id.fragmentwebLinearLayout1));

        AButton btnGoback = mView.findViewById(R.id.fragmentwebAButton1);
        btnGoback.setOnClickListener(new AButton.OnClickListener(){
                @Override
                public void onClick(View v) {
                    mBaseWebView.goBack();
                }
            });
        AButton btnGoForward = mView.findViewById(R.id.fragmentwebAButton2);
        btnGoForward.setOnClickListener(new AButton.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if (mURLAddressView.getURLAddressText().equals(mBaseWebView.getUrl())) {
                        mBaseWebView.goForward();
                    } else {
                        mBaseWebView.stopLoading();
                        mBaseWebView.loadUrl(mURLAddressView.getURLAddressText());
                    }

                }
            });
        AButton btnReload = mView.findViewById(R.id.fragmentwebAButton3);
        btnReload.setOnClickListener(new AButton.OnClickListener(){
                @Override
                public void onClick(View v) {
                    mBaseWebView.stopLoading();
                    mBaseWebView.clearCache(true);
                    mBaseWebView.reload();
                }
            });
        AButton btnStopLoading = mView.findViewById(R.id.fragmentwebAButton4);
        btnStopLoading.setOnClickListener(new AButton.OnClickListener(){
                @Override
                public void onClick(View v) {
                    mBaseWebView.stopLoading();
                }
            });

        mBaseWebView = mView.findViewById(R.id.fragmentwebBaseWebView1);
        mBaseWebView.setOnPageFinished(new BaseWebView.IOnPageFinished(){
                @Override
                public void onPageFinished(String url) {
                    mURLAddressView.setURLAddressText(url);
                }
            });

        final PullRefreshLayout layout = mView.findViewById(R.id.fragmentwebPullRefreshLayout1);
        // listen refresh event
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    // start refresh
                    mBaseWebView.stopLoading();
                    mBaseWebView.clearCache(true);
                    mBaseWebView.reload();
                    layout.setRefreshing(false);
                }
            });
        return mView;
    }

    public BaseWebView getWebView() {
        return mBaseWebView;
    }

    public void loadUrl(String szUrl) {
        if(mBaseWebView != null) {
            mBaseWebView.loadUrl(szUrl);
        }
    }
    
    public void reloadLastUrl() {
        mBaseWebView.stopLoading();
        mBaseWebView.loadUrl(mBaseWebView.getLastUrl());
    }

    @Override
    public void onDestroy() {
        if (mBaseWebView != null) {
            mBaseWebView.destroy();

            ViewParent parent = mBaseWebView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(mBaseWebView);
            }
            mBaseWebView.stopLoading();
            // 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
            mBaseWebView.getSettings().setJavaScriptEnabled(false);
            mBaseWebView.clearHistory();
            mBaseWebView.clearView();
            mBaseWebView.removeAllViews();
            mBaseWebView.destroy();
        }
        super.onDestroy();
    }
}
