package cc.winboll.studio.webpagesources.fragment;

/**
 * @Author ZhanGSKen@QQ.COM
 * @Date 2023/07/04 13:45:57
 * @Describe 网页源码视图 Fragment
 */
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.webpagesources.R;
import cc.winboll.studio.webpagesources.handler.SourcesFragmentHandler;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class SourcesFragment extends Fragment {

    public static final String TAG = "SourcesFragment";

    View mView;
    EditText mEditText;
    String mszHtmlPath;
    String mszHtmlFileName;
    static SourcesFragmentHandler _mSourcesFragmentHandler;

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

        _mSourcesFragmentHandler = new SourcesFragmentHandler(this);
        mszHtmlFileName = getString(R.string.app_name) + ".html";
        mszHtmlPath = getActivity().getExternalFilesDir(TAG) + "/" + mszHtmlFileName;

        return mView;
    }

    public void showCurrentWebPageHtml(String szHtml) {
        /*File fHtml = new File(App._fTempHtmlPath);
         StringBuffer sb = new StringBuffer();
         try {
         BufferedReader in = null;
         in = new BufferedReader(new InputStreamReader(new FileInputStream(fHtml), "UTF-8"));
         String line = "";
         while ((line = in.readLine()) != null) {
         sb.append(line);
         sb.append("\n");
         }
         mEditText.setText(sb.toString());
         } catch (IOException e) {
         LogUtils.d(TAG, "IOException : " + e.getMessage());
         }*/
        mEditText.setText(szHtml);
    }

    public void shareHtml() {
        saveHtml(mEditText.getText().toString());

        Uri uri;
        File file = new File(mszHtmlPath);
        if (Build.VERSION.SDK_INT >= 24) {//android 7.0以上
            uri = FileProvider.getUriForFile(getActivity().getApplicationContext(), getActivity().getApplicationContext().getPackageName() + ".fileprovider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        Intent shareIntent = new Intent("android.intent.action.VIEW");  
        shareIntent.setDataAndType(uri, "text/html");
        if (Build.VERSION.SDK_INT >= 24) {
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }    
        startActivity(Intent.createChooser(shareIntent, mszHtmlFileName));  
    }

    public static void sendSourcesUpdateMessage(String szHtml) {
        if (_mSourcesFragmentHandler != null) {
            Message message = _mSourcesFragmentHandler.obtainMessage(SourcesFragmentHandler.MSG_UPDATE);
            Bundle bundle = new Bundle();
            bundle.putString(SourcesFragmentHandler.BUNDLE_KEY_HTML, szHtml);
            message.setData(bundle);
            _mSourcesFragmentHandler.sendMessage(message);
        }
    }

    void saveHtml(String szHtml) {
        try {
            File fTempHtml = new File(mszHtmlPath);
            if (fTempHtml.exists()) {
                fTempHtml.delete();
            }
            BufferedWriter out = null;
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fTempHtml, false), "UTF-8"));
            out.write(szHtml);
            out.close();
        } catch (IOException e) {
            LogUtils.d(TAG, "IOException : " + e.getMessage());
        }
    }
}
