package com.uiui.zyappstore.activity;

import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.base.BaseActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class UserAgreementActivity extends BaseActivity {
    private WebView webView;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_useragreement;
    }

    @Override
    public void initView() {
        webView = findViewById(R.id.webView);

    }

    @Override
    public void initData() {
        initWebView();
        webView.loadDataWithBaseURL(null, readAsset("user_agreement.html"), "text/html", "UTF-8", null);
    }

    @Override
    protected void setListener() {

    }

    private void initWebView() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBlockNetworkImage(false);
        //webSettings.setBuiltInZoomControls(true);
        //webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setTextZoom(100);
        webSettings.setDefaultFontSize(20); //设置显示字体的大小
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        //控制webview不可点击
//        webView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return true;
//            }
//        });
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return true;
            }
        });
    }

    public String readAsset(String fileName) {
        try {
            //获取文件中的字节
            InputStream inputStream = getResources().getAssets().open(fileName);
            //将字节转换为字符
            InputStreamReader isReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            //使用bufferReader去读取内容
            BufferedReader reader = new BufferedReader(isReader);
            StringBuffer stringBuffer = new StringBuffer();
            String out = "";
            while ((out = reader.readLine()) != null) {
                stringBuffer.append(out);
                Log.e("TAG", "readAsset: " + out);
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
