package com.zztrapp.zztrapp.ui.currency.activity;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.tencent.smtt.sdk.TbsReaderView;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.databinding.ActivityTbswebViewBinding;
import com.zztrapp.zztrapp.utilcode.klogs.KLog;

import java.io.File;

/**
 * Describe TBSWebViewActivity
 * Created by Bby on 2022/7/2
 * Email bby0856@163.com
 */
public class TBSWebViewActivity extends BaseActivity {

    private ActivityTbswebViewBinding binding;
    private String filePath;
    private String fileName;
    private TbsReaderView mTbsReaderView;
    private String tbsReaderTemp = Environment.getExternalStorageDirectory() + "/TbsReaderTemp";

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityTbswebViewBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        filePath = getIntent().getStringExtra("filePath");
        fileName = getIntent().getStringExtra("fileName");
        KLog.e(filePath);
        KLog.e(filePath);
        binding.navView.navTitle.setText(fileName);
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTbsReaderView = new TbsReaderView(this, new TbsReaderView.ReaderCallback() {
            @Override
            public void onCallBackAction(Integer integer, Object o, Object o1) {

            }
        });
//        mRelativeLayout = findViewById(R.id.tbsView);
//        binding.rootView.addView(mTbsReaderView, new RelativeLayout.LayoutParams(-1, -1));
        binding.rootView.addView(mTbsReaderView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        displayFile();
    }

    @Override
    protected void initOnClick() {

    }

    private void displayFile() {

        //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
//        String bsReaderTemp = filePath;
//        File bsReaderTempFile = new File(bsReaderTemp);
//        if (!bsReaderTempFile.exists()) {
//            KLog.d("print", "准备创建/TbsReaderTemp！！");
//            boolean mkdir = bsReaderTempFile.mkdir();
//            if (!mkdir) {
//                KLog.d("print", "创建/TbsReaderTemp失败！！！！！");
//            }
//        }
//        Bundle bundle = new Bundle();
//        bundle.putString("filePath", filePath);
//        bundle.putString("tempPath", filePath);
//        boolean result = binding.tbsReaderView.preOpen(getFileType(fileName), false);
//        KLog.d("print", "查看文档---" + result);
//        mTbsReaderView.openFile(bundle);


//打开本地文件：通过 Bundle 类型参数的形式向 TbsReaderView 对象传递文件地址和另一个临时目录地址。
//        Bundle bundle = new Bundle();
//        bundle.putString("filePath", filePath);
//        bundle.putString("tempPath", Environment.getExternalStorageDirectory()
//                .getPath());
//        boolean result = mTbsReaderView.preOpen(fileName, false);
//        if (result) {
//            mTbsReaderView.openFile(bundle);
//        }

        //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
        String bsReaderTemp = tbsReaderTemp;
        File bsReaderTempFile = new File(bsReaderTemp);
        if (!bsReaderTempFile.exists()) {
            KLog.e("print" + "准备创建/TbsReaderTemp！！");
            boolean mkdir = bsReaderTempFile.mkdir();
            if (!mkdir) {
                KLog.e("print" + "创建/TbsReaderTemp失败！！！！！");
            }
        }
        Bundle bundle = new Bundle();
        bundle.putString("filePath", filePath);
        bundle.putString("tempPath", tbsReaderTemp);
        boolean result = mTbsReaderView.preOpen(getFileType(fileName), false);
        KLog.e("print" + "查看文档---" + result);
        if (result) {
            mTbsReaderView.openFile(bundle);
        } else {

        }

    }

    private String getFileType(String paramString) {
        String str = "";

        if (TextUtils.isEmpty(paramString)) {
            KLog.e("print" + "paramString---->null");
            return str;
        }
        KLog.e("print" + "paramString:" + paramString);
        int i = paramString.lastIndexOf('.');
        if (i <= -1) {
            KLog.e("print" + "i <= -1");
            return str;
        }

        str = paramString.substring(i + 1);
        KLog.e("print" + "paramString.substring(i + 1)------>" + str);
        return str;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTbsReaderView.onStop();
    }
}