package com.auto.clicker.autoclicker.view;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.data.Event;
import com.auto.clicker.autoclicker.data.EventWrapper;
import com.auto.clicker.autoclicker.data.PointEvent;
import com.auto.clicker.autoclicker.data.PositionUpdater;
import com.auto.clicker.autoclicker.data.SlideEvent;
import com.auto.clicker.autoclicker.room.SolutionWithEventsAndPoints;
import com.auto.clicker.autoclicker.room.entity.EventEntity;
import com.auto.clicker.autoclicker.room.entity.Solution;
import com.auto.clicker.autoclicker.room.entity.TouchPoint;
import com.auto.clicker.autoclicker.room.repository.EventRepository;
import com.auto.clicker.autoclicker.service.AutoClickService;
import com.auto.clicker.autoclicker.service.ForegroundService;
import com.auto.clicker.autoclicker.util.EventViewBinder;
import com.auto.clicker.autoclicker.util.PrefUtils;
import com.auto.clicker.autoclicker.util.ScreenUtils;
import com.auto.clicker.autoclicker.util.ViewUtils;

import java.util.ArrayList;
import java.util.List;

public class FloatingViewManager {
    private static final String TAG = "FloatingViewManager";
    public static final long DEBOUNCE_INTERVAL = 500;
    public static final int TOUCH_POINT_SIZE = 100;

    private final Context context;
    private final WindowManager windowManager;
    private final int screenWidth;
    private final int screenHeight;

    private final List<EventWrapper> runtimeEvents = new ArrayList<>();
    private int eventOrderCounter = 1;

    private LinearLayout multipleControlBarView;
    private WindowManager.LayoutParams multipleControlBarParams;

    private boolean isMultipleRunning = false;
    private long lastToggleTime = 0;

    private boolean areEventsVisible = true;

    private final FloatingTabDialogManager floatingTabDialogManager;

    private final EventRepository eventRepository;

    public FloatingViewManager(Context context) {
        this.context = context;
        this.windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point screenSize = ScreenUtils.getScreenSize(context);
        this.screenWidth = screenSize.x;
        this.screenHeight = screenSize.y;
        this.floatingTabDialogManager = new FloatingTabDialogManager(context);
        this.floatingTabDialogManager.setFloatingViewManager(this);
        this.eventRepository = new EventRepository(context.getApplicationContext());
        registerBroadcastReceiver();
    }

    public void showFloatingViews(int mode) throws SecurityException {
        if (!Settings.canDrawOverlays(context)) {
            throw new SecurityException("需要悬浮窗许可");
        }

        removeAllFloatingViews();
        showMultipleModeViews(mode);

        Log.d(TAG, "添加悬浮窗, 模式 = " + mode);
    }

    private void showMultipleModeViews(int mode) {
        initMultipleTouchPointView();
        initMultipleControlBar();

        if (multipleControlBarView != null) {
            windowManager.addView(multipleControlBarView, multipleControlBarParams);
        }
    }

    private void initMultipleTouchPointView() {
        runtimeEvents.clear();
        eventOrderCounter = 1;
    }

    private void initMultipleControlBar() {
        multipleControlBarView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.multiple_control_bar, null);
        multipleControlBarParams = createControlBarParams();

        setupDraggableView(multipleControlBarView, multipleControlBarParams, this::updateMoreControlBarPosition);

        setupControlButtons(multipleControlBarView);
    }

    private WindowManager.LayoutParams createControlBarParams() {
        // 创建控制栏的悬浮窗参数
        int overlayType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                WindowManager.LayoutParams.TYPE_PHONE;

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                overlayType,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT
        );
        params.gravity = Gravity.TOP | Gravity.START;
        params.x = 0;
        params.y = 200;
        return params;
    }

    private void setupDraggableView(View view, WindowManager.LayoutParams params, PositionUpdater positionUpdater) {
        view.setOnTouchListener(new View.OnTouchListener() {
            private float lastX, lastY;
            private float paramX, paramY;
            private boolean isDragging = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isMultipleRunning) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.performClick();
                        return true;
                    }
                    return false;
                }

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        paramX = params.x;
                        paramY = params.y;
                        isDragging = true;
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        if (isDragging) {
                            float dx = event.getRawX() - lastX;
                            float dy = event.getRawY() - lastY;
                            positionUpdater.update(paramX, paramY, dx, dy);
                            return true;
                        }
                        break;

                    case MotionEvent.ACTION_UP:
                        isDragging = false;
                        v.performClick();
                        return true;
                }
                return false;
            }
        });
    }

    private void updateMoreControlBarPosition(float paramX, float paramY, float dx, float dy) {
        Point constrainedPoint = ViewUtils.constrainToScreen(
                paramX + dx, paramY + dy,
                screenWidth - multipleControlBarView.getWidth(),
                screenHeight - multipleControlBarView.getHeight());
        multipleControlBarParams.x = constrainedPoint.x;
        multipleControlBarParams.y = constrainedPoint.y;
        windowManager.updateViewLayout(multipleControlBarView, multipleControlBarParams);
    }

    private void setupControlButtons(LinearLayout controlBar) {
        ImageView playButton = controlBar.findViewById(R.id.play_button);
        ImageView addButton = controlBar.findViewById(R.id.add_button);
        ImageView removeButton = controlBar.findViewById(R.id.remove_button);
        ImageView slideButton = controlBar.findViewById(R.id.slide_button);
        ImageView saveButton = controlBar.findViewById(R.id.save_button);
        ImageView eyeButton = controlBar.findViewById(R.id.eye_button);
        ImageView settingButton = controlBar.findViewById(R.id.settings_button);
        ImageView closeButton = controlBar.findViewById(R.id.close_button);

        playButton.setOnClickListener(v -> toggleMultipleClicking());
        addButton.setOnClickListener(v -> addPointEvent());
        removeButton.setOnClickListener(v -> removeLastEvent());
        slideButton.setOnClickListener(v -> addSlideEvent());

        saveButton.setOnClickListener(v -> {
            floatingTabDialogManager.showFloatingTabDialog();
        });

        eyeButton.setOnClickListener(v -> {
            areEventsVisible = !areEventsVisible;
            toggleEventsVisibility(areEventsVisible);
            if (areEventsVisible) {
                eyeButton.setImageResource(R.drawable.eye);
            } else {
                eyeButton.setImageResource(R.drawable.hide_eye);
            }
        });

        settingButton.setOnClickListener(v -> showInputDialog());
        closeButton.setOnClickListener(v -> closeFloatingViews());
    }

    private void toggleMultipleClicking() {
        if (isDebounced()) return;

        AutoClickService service = AutoClickService.getInstance();
        if (service == null) {
            handleMissingService();
            return;
        }

        if (runtimeEvents.isEmpty()) {
            Toast.makeText(context, "请添加一个触摸点或者滑动事件", Toast.LENGTH_SHORT).show();
            return;
        }

        if (isMultipleRunning) {
            stopMultiClicking(service);
        } else {
            startMultiClicking(service);
        }

        isMultipleRunning = !isMultipleRunning;
        Log.d(TAG, "多点点击服务状态: " + service.isClicking());
    }

    private void addPointEvent() {
        if (isMultipleRunning) {
            Toast.makeText(context, "请停止点击", Toast.LENGTH_SHORT).show();
            return;
        }

        PointEvent pointEvent = new PointEvent(eventOrderCounter, screenWidth / 2 - TOUCH_POINT_SIZE / 2,
                screenHeight / 2 - TOUCH_POINT_SIZE / 2);

        EventViewBinder.bindPoint(context, windowManager, pointEvent,
                TOUCH_POINT_SIZE, screenWidth, screenHeight,
                (x, y) -> updateServicePositions(), eventOrderCounter);

        runtimeEvents.add(new EventWrapper(EventWrapper.EventType.POINT, pointEvent, eventOrderCounter));
        eventOrderCounter++;
    }

    private void addSlideEvent() {
        if (isMultipleRunning) {
            Toast.makeText(context, "请先停止点击", Toast.LENGTH_SHORT).show();
            return;
        }

        PointEvent startPoint = new PointEvent(eventOrderCounter, screenWidth / 2 - 100, screenHeight / 2 - 50);
        PointEvent endPoint = new PointEvent(eventOrderCounter, screenWidth / 2 + 100, screenHeight / 2 - 50);
        SlideEvent slideEvent = new SlideEvent(eventOrderCounter, startPoint, endPoint);

        EventViewBinder.bindSlide(context, windowManager, slideEvent,
                TOUCH_POINT_SIZE, screenWidth, screenHeight,
                (x, y) -> updateServicePositions(), eventOrderCounter);

        runtimeEvents.add(new EventWrapper(EventWrapper.EventType.SLIDE, slideEvent, eventOrderCounter));
        eventOrderCounter++;
    }

    public void toggleEventsVisibility(boolean show) {
        for (EventWrapper wrapper : new ArrayList<>(runtimeEvents)) {
            // 获取事件的视图
            View viewToToggle = null;
            if (wrapper.getType() == EventWrapper.EventType.POINT) {
                viewToToggle = EventViewBinder.createTouchPointView(context, String.valueOf(wrapper.getEvent().getId()));
            } else if (wrapper.getType() == EventWrapper.EventType.SLIDE) {
                View startPointView = getView(show, wrapper, context);

                WindowManager.LayoutParams startParams = (WindowManager.LayoutParams) startPointView.getLayoutParams();
                if (show) {
                    startParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
                } else {
                    startParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
                }
                windowManager.updateViewLayout(startPointView, startParams);
                continue;
            }

            if (viewToToggle != null) {
                if (show) {
                    viewToToggle.setAlpha(1.0f);
                } else {
                    viewToToggle.setAlpha(0.0f);
                }
                WindowManager.LayoutParams params = (WindowManager.LayoutParams) viewToToggle.getLayoutParams();
                if (show) {
                    params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
                } else {
                    params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
                }
                windowManager.updateViewLayout(viewToToggle, params);
            }
        }
    }

    @NonNull
    private View getView(boolean show, EventWrapper wrapper, Context context) {
        SlideEvent slideEvent = (SlideEvent) wrapper.getEvent();
        View startPointView = EventViewBinder.createTouchPointView(context, String.valueOf(slideEvent.getStartPoint().getId()));
        View endPointView = EventViewBinder.createTouchPointView(context, String.valueOf(slideEvent.getEndPoint().getId()));
        View lineView = new ConnectingLineView(context);

        if (show) {
            startPointView.setAlpha(1.0f);
            endPointView.setAlpha(1.0f);
            lineView.setAlpha(1.0f);
        } else {
            startPointView.setAlpha(0.0f);
            endPointView.setAlpha(0.0f);
            lineView.setAlpha(0.0f);
        }
        return startPointView;
    }

    private void removeLastEvent() {
        if (isMultipleRunning) {
            Toast.makeText(context, "请先停止点击", Toast.LENGTH_SHORT).show();
            return;
        }

        if (runtimeEvents.isEmpty()) {
            Toast.makeText(context, "没有更多的事件需要删除", Toast.LENGTH_SHORT).show();
            return;
        }

        Event lastEvent = runtimeEvents.remove(runtimeEvents.size() - 1).getEvent();
        removeEvent(lastEvent);
        eventOrderCounter--;
        updateServicePositions();
    }

    private void closeFloatingViews() {
        AutoClickService service = AutoClickService.getInstance();
        if (service != null) {
            service.stopClicking();
        } else {
            Log.d(TAG, "自动点击服务没有初始化");
            Toast.makeText(context, "请同意无障碍服务权限", Toast.LENGTH_SHORT).show();
        }

        removeAllFloatingViews();

        context.stopService(new Intent(context, ForegroundService.class));
        PrefUtils.setFloatingShown(context, 0);

        Intent intent = new Intent("com.auto.clicker.autoclicker.FLOATING_WINDOW_STATE_CHANGED");
        intent.putExtra("isShown", false);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    }

    private void startMultiClicking(AutoClickService service) {
        service.addMultipleClickEvents(runtimeEvents);
        service.startClicking();

        Log.d(TAG, "开始多点点击事件 " + runtimeEvents.size());
        Toast.makeText(context, "多点点击开始", Toast.LENGTH_SHORT).show();

        updateTouchPointsBackground(R.drawable.touch_point);
    }

    private void stopMultiClicking(AutoClickService service) {
        service.stopClicking();
        Toast.makeText(context, "多点点击停止", Toast.LENGTH_SHORT).show();

        updateTouchPointsBackground(R.drawable.ring_has_bg);
    }

    private void removeEvent(Event event) {
        try {
            if (event instanceof PointEvent) {
                PointEvent pointEvent = (PointEvent) event;
                windowManager.removeView(pointEvent.getView());
            } else if (event instanceof SlideEvent) {
                SlideEvent slideEvent = (SlideEvent) event;
                windowManager.removeView(slideEvent.getStartPoint().getView());
                windowManager.removeView(slideEvent.getEndPoint().getView());
                windowManager.removeView(slideEvent.getLineView());
            }
        } catch (Exception e) {
            Log.w(TAG, "删除事件失败", e);
        }
    }

    public void removeAllFloatingViews() {
        removeAllEventViews();

        removeMultipleControlBarView();

        if (floatingTabDialogManager != null) {
            floatingTabDialogManager.removeFloatingTabDialog();
        }

        Log.d(TAG, "所有浮动视图（包括控制栏和事件）已移除");
    }

    public void removeAllEventViews() {
        try {
            for (EventWrapper wrapper : new ArrayList<>(runtimeEvents)) {
                if (wrapper.getType() == EventWrapper.EventType.POINT) {
                    PointEvent pe = (PointEvent) wrapper.getEvent();
                    safeRemoveView(pe.getView());
                } else if (wrapper.getType() == EventWrapper.EventType.SLIDE) {
                    SlideEvent se = (SlideEvent) wrapper.getEvent();
                    safeRemoveView(se.getStartPoint().getView());
                    safeRemoveView(se.getEndPoint().getView());
                    safeRemoveView(se.getLineView());
                }
            }
            runtimeEvents.clear();
            Log.d(TAG, "所有事件悬浮窗已移除");
        } catch (Exception e) {
            Log.e(TAG, "移除事件悬浮窗失败", e);
        }
    }

    public void removeMultipleControlBarView() {
        try {
            if (multipleControlBarView != null) {
                safeRemoveView(multipleControlBarView);
                multipleControlBarView = null;
                isMultipleRunning = false;
                Log.d(TAG, "多选控制栏悬浮窗已移除");
            }
        } catch (Exception e) {
            Log.e(TAG, "移除多选控制栏悬浮窗失败", e);
        }
    }

    private void safeRemoveView(View view) {
        try {
            windowManager.removeView(view);
        } catch (Exception e) {
            Log.w(TAG, "移除视图失败: " + view, e);
        }
    }

    private void updateTouchPointsBackground(int resourceId) {
        for (EventWrapper wrapper : new ArrayList<>(runtimeEvents)) {
            if (wrapper.getType() == EventWrapper.EventType.POINT) {
                PointEvent pe = (PointEvent) wrapper.getEvent();
                pe.getView().setBackgroundResource(resourceId);
            }
        }

        for (EventWrapper wrapper : new ArrayList<>(runtimeEvents)) {
            if (wrapper.getType() == EventWrapper.EventType.SLIDE) {
                SlideEvent se = (SlideEvent) wrapper.getEvent();
                se.getStartPoint().getView().setBackgroundResource(resourceId);
                se.getEndPoint().getView().setBackgroundResource(resourceId);
            }
        }
    }

    private void handleMissingService() {
        Log.e(TAG, "自动点击服务没有初始化");
        Toast.makeText(context, "请在设置里同意无障碍服务", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private void updateServicePositions() {
        AutoClickService service = AutoClickService.getInstance();
        if (service != null) {
            service.addMultipleClickEvents(runtimeEvents);
        }
    }

    public void showInputDialog() {
        EditText input = new EditText(context);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        input.setHint("输入点击间隔（ms）");
        input.setFocusableInTouchMode(true);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("设置点击间隔")
                .setView(input)
                .setPositiveButton("确定", (dialog, which) -> {
                    String value = input.getText().toString();
                    try {
                        long interval = Long.parseLong(value);
                        AutoClickService service = AutoClickService.getInstance();
                        if (service != null) {
                            service.setClickInterval(interval);
                        } else {
                            Log.w("InputDialog", "AutoClickService 未初始化");
                        }
                    } catch (NumberFormatException e) {
                        Toast.makeText(context, "无效输入", Toast.LENGTH_SHORT).show();
                    }
                    dialog.dismiss();
                })
                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss());

        AlertDialog dialog = builder.create();

        if (!(context instanceof Activity)) {
            if (dialog.getWindow() != null) {
                dialog.getWindow().setType(
                        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                                WindowManager.LayoutParams.TYPE_PHONE
                );
            }
        }

        dialog.show();

        input.requestFocus();
        input.post(() -> {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
        });
    }

    public void flashTouchPoint(int index) {
        if (index >= 0 && index < runtimeEvents.size()) {
            Event event = runtimeEvents.get(index).getEvent();
            Log.d(TAG, "index : " + index);

            if (event instanceof PointEvent) {
                flashView(((PointEvent) event).getView());
            } else if (event instanceof SlideEvent) {
                SlideEvent slide = (SlideEvent) event;
                flashView(slide.getStartPoint().getView());
                flashView(slide.getEndPoint().getView());
            }
        }
    }

    private void flashView(View view) {
        if (view != null) {
            view.animate()
                    .alpha(0.3f)
                    .setDuration(100)
                    .withEndAction(() -> view.animate()
                            .alpha(1.0f)
                            .setDuration(100)
                            .start())
                    .start();
        }
    }

    private void registerBroadcastReceiver() {
        LocalBroadcastManager.getInstance(context).registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        if ("com.auto.clicker.autoclicker.FLASH_TOUCH_POINT".equals(intent.getAction())) {
                            int index = intent.getIntExtra("index", -1);
                            flashTouchPoint(index);
                        }
                    }
                },
                new IntentFilter("com.auto.clicker.autoclicker.FLASH_TOUCH_POINT")
        );
    }

    public void saveCurrentEventsAsSolution(String solutionName, EventRepository.RepositoryCallback<Long> callback) {
        if (solutionName == null || solutionName.trim().isEmpty()) {
            Toast.makeText(context, "方案名称不能为空", Toast.LENGTH_SHORT).show();
            // 在这里调用回调的 onError，或者 onComplete 返回一个表示失败的值
            // 确保 FloatingTabDialogManager 收到反馈
            if (callback != null) {
                callback.onComplete(-1L); // 返回一个表示失败的值
            }
            return;
        }
        if (runtimeEvents.isEmpty()) {
            Toast.makeText(context, "当前没有事件可以保存", Toast.LENGTH_SHORT).show();
            if (callback != null) {
                callback.onComplete(-1L); // 返回一个表示失败的值
            }
            return;
        }

        Solution newSolution = new Solution(solutionName);
        eventRepository.insertSolutionWithEvents(newSolution, runtimeEvents, new EventRepository.RepositoryCallback<Long>() {
            @Override
            public void onComplete(Long solutionId) {
                // 成功保存，Toast 可以在这里显示，也可以在 FloatingTabDialogManager 中显示
                // 为了职责分离，建议在这里显示一个简单的"保存成功" Toast，然后让 FloatingTabDialogManager 决定是否关闭
                // Toast.makeText(context, "方案 '" + solutionName + "' 保存成功！", Toast.LENGTH_SHORT).show();
                // 保存失败，Toast 可以在这里显示
                // Toast.makeText(context, "保存方案失败。", Toast.LENGTH_SHORT).show();
                if (callback != null) {
                    callback.onComplete(solutionId); // 将结果传递给调用者
                }
            }

            @Override
            public void onError(Exception e) {
                Log.e(TAG, "保存方案失败: " + e.getMessage(), e);
                Toast.makeText(context, "保存方案失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                if (callback != null) {
                    callback.onError(e); // 将错误传递给调用者
                }
            }
        });
    }

    public void loadSolution(long solutionId) {
        eventRepository.getSolutionWithEventsAndPoints(solutionId, new EventRepository.RepositoryCallback<SolutionWithEventsAndPoints>() {
            @Override
            public void onComplete(SolutionWithEventsAndPoints solutionData) {
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (solutionData != null && solutionData.events != null) {
                        removeAllEventViews();
                        runtimeEvents.clear();
                        eventOrderCounter = 1;

                        for (SolutionWithEventsAndPoints.EventWithPoints eventWithPoints : solutionData.events) {
                            EventEntity eventEntity = eventWithPoints.eventEntity;
                            EventWrapper.EventType type = EventWrapper.EventType.valueOf(eventEntity.eventType);

                            if (type == EventWrapper.EventType.POINT) {
                                TouchPoint clickPoint = eventWithPoints.clickTouchPoint;
                                if (clickPoint != null) {
                                    PointEvent pointEvent = new PointEvent(eventEntity.orderInSolution, clickPoint.x, clickPoint.y);

                                    EventViewBinder.bindPoint(context, windowManager,
                                            pointEvent,
                                            TOUCH_POINT_SIZE,
                                            screenWidth, screenHeight,
                                            (x, y) -> updateServicePositions(), eventEntity.orderInSolution);

                                    runtimeEvents.add(new EventWrapper(type, pointEvent, pointEvent.getId()));
                                }
                            } else if (type == EventWrapper.EventType.SLIDE) {
                                TouchPoint startPointData = eventWithPoints.slideStartTouchPoint;
                                TouchPoint endPointData = eventWithPoints.slideEndTouchPoint;

                                if (startPointData != null && endPointData != null) {
                                    PointEvent startPointEvent = new PointEvent(eventEntity.orderInSolution, startPointData.x, startPointData.y);
                                    PointEvent endPointEvent = new PointEvent(eventEntity.orderInSolution, endPointData.x, endPointData.y);

                                    SlideEvent slideEvent = new SlideEvent(eventEntity.orderInSolution, startPointEvent, endPointEvent);

                                    EventViewBinder.bindSlide(context, windowManager,
                                            slideEvent,
                                            TOUCH_POINT_SIZE,
                                            screenWidth, screenHeight,
                                            (x, y) -> updateServicePositions(), eventEntity.orderInSolution);

                                    runtimeEvents.add(new EventWrapper(type, slideEvent, slideEvent.getId()));
                                }
                            }

                            if (eventEntity.orderInSolution >= eventOrderCounter) {
                                eventOrderCounter = eventEntity.orderInSolution + 1;
                            }
                        }
                        updateServicePositions();
                        Toast.makeText(context, "方案 '" + solutionData.solution.solutionName + "' 加载成功！", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, "加载方案失败或方案不存在。", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onError(Exception e) {
                Log.e(TAG, "加载方案失败: " + e.getMessage(), e);
                new Handler(Looper.getMainLooper()).post(() ->
                        Toast.makeText(context, "加载方案失败: " + e.getMessage(), Toast.LENGTH_SHORT).show()
                );
            }
        });
    }

    public void getAllSolutions(EventRepository.RepositoryCallback<List<Solution>> callback) {
        if (eventRepository != null) {
            eventRepository.getAllSolutions(callback);
        } else {
            callback.onError(new IllegalStateException("EventRepository not initialized."));
        }
    }

    private boolean isDebounced() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastToggleTime < DEBOUNCE_INTERVAL) {
            return true;
        }
        lastToggleTime = currentTime;
        return false;
    }
}