package com.auto.clicker.autoclicker.service;

import static com.auto.clicker.autoclicker.ui.activity.main.MainActivity.FLOATING_SINGLE;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.ui.activity.main.MainActivity;
import com.auto.clicker.autoclicker.view.FloatingViewManager;

public class ForegroundService extends Service {
    private static final String TAG = "ForegroundService";
    private static final String CHANNEL_ID = "AutoClickerChannel";
    private static final int NOTIFICATION_ID = 1;
    private FloatingViewManager floatingViewManager;
    private boolean isFloatingViewShown = false;

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
        try {
            floatingViewManager = new FloatingViewManager(this);
        } catch (Exception e) {
            Log.e(TAG, "初始化 FloatingViewManager 失败", e);
            stopSelf();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int type = intent != null ? intent.getIntExtra("FLOATING_TYPE", FLOATING_SINGLE) : FLOATING_SINGLE;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("自动点击运行中")
                .setContentText("自动点击正在后台运行")
                .setSmallIcon(R.drawable.notification)
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .build();

        startForeground(NOTIFICATION_ID, notification);

        if (floatingViewManager != null && !isFloatingViewShown) {
            try {
                floatingViewManager.showFloatingViews(type);
                isFloatingViewShown = true;
            } catch (SecurityException e) {
                Log.e(TAG, "未授予悬浮窗权限", e);
                stopSelf();
            }
        } else if (floatingViewManager == null) {
            stopSelf();
        }

        return START_STICKY;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "自动点击服务",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel.setDescription("自动点击前台服务通知渠道");
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (floatingViewManager != null) {
            floatingViewManager.removeFloatingViews();
            isFloatingViewShown = false;
            Log.d(TAG, "悬浮窗已移除");
        }
        stopForeground(true);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}