package com.unibuy.smartdevice.worker;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;

import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.ui.FontendActivity;

import java.io.DataOutputStream;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class BootReceiver extends BroadcastReceiver {
//    @Override
//    public void onReceive(Context context, Intent intent) {
//        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ||
//                Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction()) ||
//                Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) {
//            Intent launchIntent = new Intent(context, FontendActivity.class);
//            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(launchIntent);
//        }
//    }

    Logs logs = new Logs(getClass());

    @Override
    public void onReceive(Context context, Intent intent) {
        new Thread(new Runnable() {
            @Override
            public void run() {


                try {
                    // 確保 context 和 intent 不為 null
                    if (context == null || intent == null) {
                        logs.debug("Context or Intent is null, cannot process boot event.");
                        return;
                    }

                    // 檢查 intent action 是否符合期望
                    String action = intent.getAction();
                    if (Intent.ACTION_BOOT_COMPLETED.equals(action) ||
                            Intent.ACTION_PACKAGE_REPLACED.equals(action) ||
                            Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) {

                        // 防止重複啟動活動
                        if (isActivityRunning(context, FontendActivity.class)) {
                            logs.debug("FontendActivity is already running, no need to start again.");
                            return;
                        }

                        // 啟動 FontendActivity
                        Intent launchIntent = new Intent(context, FontendActivity.class);
                        launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(launchIntent);
                        logs.info("FontendActivity started after boot or package replaced.");
//                        logs.info("BootReceiver 開機完成，啟用Ethernet tethering");
//                        new Handler(Looper.getMainLooper()).postDelayed(() -> {
//                            EthernetTetherHelper.enableEthernetTethering();
//                        }, 5000);  // 延遲5秒
//                        logs.info("BootReceiver 啟用Ethernet tethering完成");
                    }
                } catch (Exception e) {
                    // 錯誤處理，記錄錯誤資訊
                    logs.warning(e);
                }
            }
        }).start();  // 在背景線程中啟動
    }

    // 檢查活動是否正在運行
    private boolean isActivityRunning(Context context, Class<?> activityClass) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager != null) {
            List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
            for (ActivityManager.RunningTaskInfo task : tasks) {
                if (activityClass.getName().equals(task.baseActivity.getClassName())) {
                    return true;
                }
            }
        }
        return false;
    }


//        public void listAllNetworkInterfaces() {
//            try {
//                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
//                for (NetworkInterface intf : Collections.list(interfaces)) {
//                    String name = intf.getName();
//                    String displayName = intf.getDisplayName();
//                    logs.info("介面名稱: " + name + " / 顯示名稱: " + displayName);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }


}
