package cc.winboll.studio.winboll.applications;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.winboll.R;
import cc.winboll.studio.winboll.termux.TermuxCommandExecutor;

public class MyTermuxActivity extends AppCompatActivity {

    public static final String TAG = "MyTermuxActivity";

    private Toolbar mToolbar;
    private Button mTermuxButton;
	private Button mTermuxWorkSpacesButton;

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

        // 初始化工具栏
        initToolbar();
        // 初始化按钮
        initView();
    }

    private void initToolbar() {
        mToolbar = findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						LogUtils.d(TAG, "点击返回按钮");
						finish();
					}
				});
            LogUtils.d(TAG, "工具栏初始化完成");
        }
    }

    private void initView() {
        mTermuxButton = findViewById(R.id.btn_termux);
        if (mTermuxButton != null) {
            mTermuxButton.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						LogUtils.d(TAG, "点击 Termux 按钮");
						TermuxCommandExecutor.openTermuxBash(MyTermuxActivity.this, "cd ~");
						//TermuxCommandExecutor.openTermuxBash(MyTermuxActivity.this, "cd ~/TermuxWorkSpaces", "./TermuxWorkSpaces");
					}
				});
            LogUtils.d(TAG, "Termux 按钮初始化完成");
        }

		mTermuxWorkSpacesButton = findViewById(R.id.btn_termuxworkspaces);
        if (mTermuxWorkSpacesButton != null) {
            mTermuxWorkSpacesButton.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						LogUtils.d(TAG, "点击 TermuxWorkSpaces 按钮");
						TermuxCommandExecutor.openTermuxBash(MyTermuxActivity.this, "cd ~/TermuxWorkSpaces", "./TermuxWorkSpaces");
					}
				});
            LogUtils.d(TAG, "TermuxWorkSpaces 按钮初始化完成");
        }
    }

    private boolean isTermuxAvailable() {
        return TermuxCommandExecutor.isTermuxInstalled(this);
    }
}
