package com.grigowashere.aismap.ui;

import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import com.grigowashere.aismap.R;
import com.grigowashere.aismap.controllers.AppCoordinator;
import com.grigowashere.aismap.utils.SettingsManager;

/**
 * Отвечает за работу с меню в MainActivity: создание и обработка пунктов
 */
public class MenuBinder {

    private static final String TAG = "MenuBinder";
    private final AppCoordinator appCoordinator;
    private final SettingsManager settingsManager;
    private final MenuActions actions;

    public MenuBinder(AppCoordinator appCoordinator, SettingsManager settingsManager, MenuActions actions) {
        this.appCoordinator = appCoordinator;
        this.settingsManager = settingsManager;
        this.actions = actions;
    }

	public boolean onCreateOptionsMenu(Menu menu) {
		// Пока без переноса инфлейта; оставим точку расширения
		Log.d(TAG, "onCreateOptionsMenu: ready");
		return true;
	}

    public boolean onPrepareOptionsMenu(Menu menu) {
        try {
            MenuItem gpsItem = menu.findItem(R.id.menu_gps);
            MenuItem udpItem = menu.findItem(R.id.menu_udp);
            if (gpsItem != null) {
                gpsItem.setTitle(appCoordinator.isAndroidNMEAEnabled() ? "GPS ✓" : "GPS");
            }
            if (udpItem != null) {
                udpItem.setTitle(appCoordinator.isUDPEnabled() ? "UDP ✓" : "UDP");
            }
            MenuItem pathItem = menu.findItem(R.id.menu_path_tracking);
            if (pathItem != null) {
                boolean pathEnabled = settingsManager.isPathTrackingEnabled();
                pathItem.setTitle(pathEnabled ? "Пути ✓" : "Пути");
            }
            MenuItem screenItem = menu.findItem(R.id.menu_keep_screen_on);
            if (screenItem != null) {
                boolean screenEnabled = settingsManager.isKeepScreenOnEnabled();
                screenItem.setTitle(screenEnabled ? "Экран ✓" : "Экран");
            }
            MenuItem seamarksItem = menu.findItem(R.id.menu_seamarks);
            if (seamarksItem != null) {
                boolean seamarksEnabled = settingsManager.isSeamarksEnabled();
                seamarksItem.setTitle(seamarksEnabled ? "Морские знаки ✓" : "Морские знаки");
            }
            return true;
        } catch (Exception e) {
            Log.w(TAG, "onPrepareOptionsMenu: " + e.getMessage());
            return false;
        }
    }

	public boolean onOptionsItemSelected(MenuItem item) {
        if (item == null) return false;
        int id = item.getItemId();
        try {
            if (id == R.id.menu_gps) {
                actions.toggleGPS();
                return true;
            } else if (id == R.id.menu_udp) {
                actions.toggleUDP();
                return true;
            } else if (id == R.id.menu_clear_ais) {
                actions.clearAIS();
                return true;
            } else if (id == R.id.menu_path_tracking) {
                actions.togglePathTracking();
                return true;
            } else if (id == R.id.menu_service_test) {
                actions.testForegroundService();
                return true;
            } else if (id == R.id.menu_keep_screen_on) {
                actions.toggleKeepScreenOn();
                return true;
            } else if (id == R.id.menu_seamarks) {
                actions.toggleSeamarks();
                return true;
            }
        } catch (Exception e) {
            Log.w(TAG, "onOptionsItemSelected error: " + e.getMessage());
        }
        return false;
	}

    /**
     * Действия меню, которые реализует MainActivity
     */
    public interface MenuActions {
        void toggleGPS();
        void toggleUDP();
        void clearAIS();
        void togglePathTracking();
        void testForegroundService();
        void toggleKeepScreenOn();
        void toggleSeamarks();
    }
}


