package com.grigowashere.aismap.ui;
import android.content.Context;
import android.util.Log;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.grigowashere.aismap.models.AISVessel;
import com.grigowashere.aismap.models.Vessel;
import com.grigowashere.aismap.R;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class BottomSheetsBinder {
	private static final String TAG = "BottomSheetsBinder";
    private final Context context;

	private BottomSheetDialog ownVesselBottomSheet;
	private BottomSheetDialog aisVesselBottomSheet;
	private View ownBottomSheetView;
	private View aisBottomSheetView;
	private AISVessel currentAISVessel;

	// Auto update
	private android.os.Handler updateHandler;
	private Runnable updateRunnable;
	private int updateIntervalMs = 1000;

    public BottomSheetsBinder(Context context) {
        this.context = context;
        this.updateHandler = new android.os.Handler(android.os.Looper.getMainLooper());
    }

	public void setUpdateIntervalMs(int intervalMs) {
		this.updateIntervalMs = Math.max(250, intervalMs);
	}

	public void init(View ownBottomSheetView) {
		this.ownBottomSheetView = ownBottomSheetView;
		ownVesselBottomSheet = new BottomSheetDialog(context);
		ownVesselBottomSheet.setContentView(ownBottomSheetView);
		ownVesselBottomSheet.setCanceledOnTouchOutside(true);
		ownVesselBottomSheet.setCancelable(true);
	}

	public void initAIS(View aisView) {
		this.aisBottomSheetView = aisView;
		aisVesselBottomSheet = new BottomSheetDialog(context);
		aisVesselBottomSheet.setContentView(aisBottomSheetView);
		aisVesselBottomSheet.setCanceledOnTouchOutside(true);
		aisVesselBottomSheet.setCancelable(true);
		ImageButton btnCloseAIS = aisBottomSheetView.findViewById(R.id.btn_close_ais_bottom_sheet);
		if (btnCloseAIS != null) {
			btnCloseAIS.setOnClickListener(v -> aisVesselBottomSheet.dismiss());
		}
	}

    public void showOwnVesselSheet() {
		if (ownVesselBottomSheet != null && !ownVesselBottomSheet.isShowing()) {
            // Обновление UI делегируется вызывающей стороне при необходимости
			ownVesselBottomSheet.show();
			startAutoUpdate();
		}
	}

    public void showOwnVesselSheet(Runnable onUpdateStart) {
        if (ownVesselBottomSheet != null && !ownVesselBottomSheet.isShowing()) {
            if (onUpdateStart != null) onUpdateStart.run();
            ownVesselBottomSheet.show();
            startAutoUpdate();
        }
    }

    public void showAISVesselSheet(AISVessel vessel) {
		this.currentAISVessel = vessel;
		if (aisVesselBottomSheet != null && !aisVesselBottomSheet.isShowing() && vessel != null) {
            // Обновление UI делегируется вызывающей стороне при необходимости
			aisVesselBottomSheet.show();
			startAutoUpdate();
		}
	}

    public void showAISVesselSheet(AISVessel vessel, Runnable onUpdateStart) {
        this.currentAISVessel = vessel;
        if (aisVesselBottomSheet != null && !aisVesselBottomSheet.isShowing() && vessel != null) {
            if (onUpdateStart != null) onUpdateStart.run();
            aisVesselBottomSheet.show();
            startAutoUpdate();
        }
    }

	public void startAutoUpdate() {
		if (updateRunnable != null) {
			updateHandler.removeCallbacks(updateRunnable);
		}
		updateRunnable = new Runnable() {
			@Override public void run() {
				try {
                    // Обновление контента выполняется стороной-владельцем
				} finally {
					updateHandler.postDelayed(this, updateIntervalMs);
				}
			}
		};
		updateHandler.postDelayed(updateRunnable, updateIntervalMs);
	}

	public void stopAutoUpdate() {
		if (updateRunnable != null) {
			updateHandler.removeCallbacks(updateRunnable);
		}
	}

    public void updateOwnVesselUI() { /* делегируется владельцу */ }

    public void updateAISUI(AISVessel vessel) { /* делегируется владельцу */ }

    public void updateAISTimeAgo() { /* делегируется владельцу */ }

    private String safe(String s) { return s==null?"--":s; }
}


