package com.unibuy.smartdevice.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.unibuy.smartdevice.AppCompatActivityAbstract;
import com.unibuy.smartdevice.MainActivity;
import com.unibuy.smartdevice.MyApp;
import com.unibuy.smartdevice.R;
import com.unibuy.smartdevice.external.StarCloudAPI;
import com.unibuy.smartdevice.tools.HandlerMain;
import com.unibuy.smartdevice.tools.ToastHandlerMain;

public class MaintenanceActivity extends AppCompatActivityAbstract {

    @Override
    protected Context setCtx() {
        return this;
    }

    @Override
    protected Class<?> setCls() {
        return getClass();
    }

    @Override
    protected HandlerMain setHandlerMain() {
        return new ToastHandlerMain(getCtx(), getLogs());
    }

    private Handler longPressHandler = new Handler();
    private Runnable longPressRunnable = () -> {
        showLoginDialog(MaintenanceActivity.this);
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 設定全螢幕
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);

        setContentView(R.layout.activity_maintenance);

        View backdoorView = findViewById(R.id.viewBackdoor);
        backdoorView.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // 長按 5 秒觸發後門
                    longPressHandler.postDelayed(longPressRunnable, 5000);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    longPressHandler.removeCallbacks(longPressRunnable);
                    break;
            }
            return true;
        });
    }

    @Override
    public void onBackPressed() {
        // 封鎖返回鍵，不執行任何動作
    }

    public void showLoginDialog(Context context) {
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(50, 20, 50, 10);

        final EditText accessInput = new EditText(context);
        accessInput.setHint("帳號");
        layout.addView(accessInput);

        final EditText passwordInput = new EditText(context);
        passwordInput.setHint("密碼");
        passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        layout.addView(passwordInput);

        new AlertDialog.Builder(context)
                .setTitle("後門登入")
                .setView(layout)
                .setPositiveButton("登入", (dialog, which) -> {
                    String access = accessInput.getText().toString().trim();
                    String password = passwordInput.getText().toString().trim();

                    if (access.isEmpty() || password.isEmpty()) {
                        Toast.makeText(context, "請輸入帳號和密碼", Toast.LENGTH_SHORT).show();
                    } else {
                        // 初始化 StarCloudAPI (必須傳入正確的 HandlerMain)
                        StarCloudAPI starCloudAPI = new StarCloudAPI(getHandlerMain(), getHandlerMain());
                        starCloudAPI.login(access, password);
                        
                        // 這裡我們假設登入動作會處理後續跳轉，
                        // 或者我們可以手動跳轉回 MainActivity 供工程師操作
                        Intent intent = new Intent(MaintenanceActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}
