package com.dragonssh.xhttpdemo;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dragonssh.xhttpdemo.core.logger.LogItem;
import com.dragonssh.xhttpdemo.core.logger.SkStatus;
import com.google.android.material.switchmaterial.SwitchMaterial;

public final class LogsActivity extends AppCompatActivity implements SkStatus.LogListener {
    private RecyclerView logList;
    private LogAdapter logAdapter;
    private SwitchMaterial followLogsSwitch;
    private SwitchMaterial showDebugLogsSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logs);
        WindowInsetsHelper.apply(this, R.id.logs_root);

        logList = findViewById(R.id.log_list);
        followLogsSwitch = findViewById(R.id.follow_logs);
        showDebugLogsSwitch = findViewById(R.id.show_debug_logs);

        logAdapter = new LogAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setStackFromEnd(true);
        logList.setLayoutManager(layoutManager);
        logList.setAdapter(logAdapter);
        logList.setItemAnimator(null);

        findViewById(R.id.back).setOnClickListener(v -> finish());
        findViewById(R.id.copy_logs).setOnClickListener(v -> copyLogs());
        findViewById(R.id.clear_logs).setOnClickListener(v -> SkStatus.clearLog());
        showDebugLogsSwitch.setOnCheckedChangeListener((button, checked) -> {
            logAdapter.setShowDebug(checked);
            if (followLogsSwitch.isChecked()) scrollLogsToBottom();
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        SkStatus.addLogListener(this);
        logAdapter.replace(SkStatus.getlogbuffer());
        scrollLogsToBottom();
    }

    @Override
    protected void onStop() {
        SkStatus.removeLogListener(this);
        super.onStop();
    }

    @Override
    public void newLog(LogItem logItem) {
        runOnUiThread(() -> {
            logAdapter.add(logItem);
            if (followLogsSwitch.isChecked()) scrollLogsToBottom();
        });
    }

    @Override
    public void onClear() {
        runOnUiThread(logAdapter::clear);
    }

    private void scrollLogsToBottom() {
        if (logAdapter == null || logAdapter.getItemCount() == 0) return;
        logList.post(() -> logList.scrollToPosition(logAdapter.getItemCount() - 1));
    }

    private void copyLogs() {
        if (logAdapter.isEmpty()) {
            Toast.makeText(this, R.string.no_logs_to_copy, Toast.LENGTH_SHORT).show();
            return;
        }
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText(
                "DragonSSH XHTTP logs", logAdapter.asPlainText()));
        Toast.makeText(this, R.string.logs_copied, Toast.LENGTH_SHORT).show();
    }
}
