package com.hgsoft.debuglogshow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;

public class DebugLogActivity extends Activity implements DebugLogService.Client.Callback {

    public final static String TAG = "DebugLogActivity";
    private DebugLogService.Client mClient = null;
    private Button mStartButton = null;
    private Button mStopButton = null;
    private Button mCopyButton = null;
    private Button mClearButton = null;
    private Button mSaveButton = null;
    private Button mTestButton = null;
    private Button mBackLastView = null;
    private ListView mLogView;
    private ArrayList<Spanned> mLogList = null;
    private ArrayAdapter<Spanned> mLogAdapter;
    private String logEndsWith = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.debug_log);

        mStartButton = (Button)findViewById(R.id.start_log);
        mStopButton = (Button)findViewById(R.id.stop_log);
        mLogView = (ListView) findViewById(R.id.log_list);
        mCopyButton = (Button)findViewById(R.id.copy_to_clipboard);
        mClearButton = (Button)findViewById(R.id.clear_log);
        mSaveButton = (Button)findViewById(R.id.save_to_file);
        mTestButton = (Button)findViewById(R.id.test_log);
        mBackLastView = (Button)findViewById(R.id.back_last_view);

        if (!TextUtils.isEmpty(getIntent().getStringExtra("logEndWith"))) {
            DebugUtil.saveSetting(DebugLogActivity.this, "logEndWith", getIntent().getStringExtra("logEndWith"));
        }

        mClient = new DebugLogService.Client(this, this);

        mStartButton.setEnabled(false);
        mStopButton.setEnabled(false);
        setOptionsButtonsEnabled(false);

        mStartButton.setOnClickListener(mStartClickListener);
        mStopButton.setOnClickListener(mStopClickListener);
        mClearButton.setOnClickListener(mClearClickListener);
        mSaveButton.setOnClickListener(mSaveClickListener);

        mCopyButton.setOnClickListener(mCopyClickListener);

        mTestButton.setOnClickListener(mTestClickListener);

        mBackLastView.setOnClickListener(mBackClickListener);
    }

    @Override
    protected void onDestroy() {
        mClient.release();
        super.onDestroy();
    }

    private View.OnClickListener mStartClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mStartButton.setEnabled(false);
            mStopButton.setEnabled(false);
            mClient.start();
        }
    };

    private View.OnClickListener mStopClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mStartButton.setEnabled(false);
            mStopButton.setEnabled(false);
            mClient.stop();
        }
    };

    private void setOptionsButtonsEnabled(boolean enabled) {
        mClearButton.setEnabled(enabled);
        mCopyButton.setEnabled(enabled);
        mSaveButton.setEnabled(enabled);
    }

    private View.OnClickListener mClearClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mClient.clear();
            if (mLogList != null) {
                mLogList.clear();
                mLogAdapter.notifyDataSetChanged();
            }
            setOptionsButtonsEnabled(false);
        }
    };

    private View.OnClickListener mSaveClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mClient.save();
        }
    };

    @SuppressWarnings("deprecation")
    private View.OnClickListener mCopyClickListener = new View.OnClickListener() {
        @SuppressLint("WrongConstant")
        @Override
        public void onClick(View v) {
            final StringBuffer buffer = new StringBuffer();
            for (Spanned line : mLogList) {
                buffer.append(line.toString()).append("\n");
            }

            android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getApplicationContext().getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(buffer);

            Snackbar.make(v.getRootView(), R.string.copied_to_clipboard, Snackbar.LENGTH_SHORT).show();
        }
    };


    private View.OnClickListener mTestClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v(TAG + logEndsWith, "测试日志-v");
            Log.d(TAG + logEndsWith, "测试日志-d");
            Log.i(TAG + logEndsWith, "测试日志-i");
            Log.w(TAG + logEndsWith, "测试日志-w");
            Log.e(TAG + logEndsWith, "测试日志-e");
        }
    };

    private View.OnClickListener mBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    };

    @Override
    public void onStarted(List<String> logList) {
        logEndsWith = DebugUtil.getSetting(DebugLogActivity.this, "logEndWith", "");
        mStartButton.setEnabled(false);
        mStopButton.setEnabled(true);
        if (logList.size() > 0) {
            setOptionsButtonsEnabled(true);
        }
        mLogList = new ArrayList<Spanned>(getSpannedList(logList));
        mLogAdapter = new ArrayAdapter<Spanned>(this, R.layout.debug_log_item, mLogList);
        mLogView.setAdapter(mLogAdapter);
        mLogView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
        if (mLogList.size() > 0) {
            mLogView.setSelection(mLogList.size() - 1);
        }
    }

    @Override
    public void onStopped() {
        mStartButton.setEnabled(true);
        mStopButton.setEnabled(false);
    }

    @Override
    public void onLog(String msg) {
        if (mLogList != null && msg.contains(logEndsWith)) {
            mLogList.add(getSpanned(msg));
            mLogAdapter.notifyDataSetChanged();
            setOptionsButtonsEnabled(true);
        }
    }

    private List<Spanned> getSpannedList(List<String> loglist) {
        List<Spanned> logSpannedList = new ArrayList<>();
        if (loglist != null && loglist.size() > 0) {
            for (String log : loglist) {
                if (log.contains(logEndsWith)) {
                    logSpannedList.add(getSpanned(log));
                }
            }
        }
        return logSpannedList;
    }

    private Spanned getSpanned(String msg) {
        if (msg.contains(" V ")) {
            String html = "<b><font color=\"#5D8C00\">" + msg + "</font></b>";
            return Html.fromHtml(html);
        } else if (msg.contains(" D ")) {
            String html = "<b><font color=\"#0CB2B3\">" + msg + "</font></b>";
            return Html.fromHtml(html);
        } else if (msg.contains(" I ")) {
            String html = "<b><font color=\"#57BF4B\">" + msg + "</font></b>";
            return Html.fromHtml(html);
        } else if (msg.contains(" W ")) {
            String html = "<b><font color=\"#BF9F03\">" + msg + "</font></b>";
            return Html.fromHtml(html);
        } else if (msg.contains(" E ")) {
            String html = "<b><font color=\"#CC1921\">" + msg + "</font></b>";
            return Html.fromHtml(html);
        } else {
            String html = "<b><font color=\"#CC4141\">" + msg + "</font></b>";
            return Html.fromHtml(html);
        }
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onSaved(boolean success, String path) {
        if (success) {
            Snackbar.make(getWindow().getDecorView().findViewById(android.R.id.content), String.format(
                    getString(R.string.dump_logcat_success),
                    path), Snackbar.LENGTH_LONG).show();
        } else {
            Snackbar.make(getWindow().getDecorView().findViewById(android.R.id.content), R.string.dump_logcat_failure, Snackbar.LENGTH_SHORT).show();
        }
    }
}
