package com.arvindcodex.codexvertex;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView txtDescription, txtFooter;
    private ImageView imgStatus;
    private Button btnPermission;

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

        txtDescription = findViewById(R.id.txtDescription);
        txtFooter = findViewById(R.id.txtStatusFooter);
        imgStatus = findViewById(R.id.imgStatus);
        btnPermission = findViewById(R.id.btnPermission);

        // Explicitly start our Foreground Service
        startService(new Intent(this, NotificationService.class));

        btnPermission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isNotificationServiceEnabled()) {
                    startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
                } else {
                    Toast.makeText(MainActivity.this, "Active & Ready!", Toast.LENGTH_SHORT).show();
                    updateUIStatus(true);
                }
            }
        });

        // Test Connectivity on Title click
        findViewById(R.id.txTitle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                testPing();
            }
        });
    }

    private void testPing() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    java.net.URL url = new java.net.URL("https://payment.winto.in/backend/create_order.php");
                    java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    final int code = conn.getResponseCode();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "PING SUCCESS! Server Code: " + code, Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
                } catch (final Exception e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "PING FAILED: " + e.getMessage(), Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUIStatus(isNotificationServiceEnabled());
    }

    private void updateUIStatus(boolean enabled) {
        if (enabled) {
            txtDescription.setText("Status: Listener is Active ✅");
            txtFooter.setText("Foreground Status: Running 24/7");
            imgStatus.setImageResource(android.R.drawable.presence_online);
            imgStatus.setImageTintList(ColorStateList.valueOf(Color.parseColor("#27ae60")));
            btnPermission.setText("Listener Ready");
            btnPermission.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#27ae60")));
        } else {
            txtDescription.setText("Status: Inactive - Grant permission below");
            txtFooter.setText("Listener Status: Inactive");
            imgStatus.setImageResource(android.R.drawable.ic_dialog_info);
            imgStatus.setImageTintList(ColorStateList.valueOf(Color.parseColor("#e74c3c")));
            btnPermission.setText("Grant Notification Access");
            btnPermission.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#3498db")));
        }
    }

    private boolean isNotificationServiceEnabled() {
        String pkgName = getPackageName();
        String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
        return flat != null && flat.contains(pkgName);
    }
}
