package com.auto.clicker.autoclicker.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.data.PointEvent;
import com.auto.clicker.autoclicker.data.SlideEvent;
import com.auto.clicker.autoclicker.view.ConnectingLineView;

public class EventViewBinder {

    public interface OnPositionChangedCallback {
        void onPositionChanged(int x, int y);
    }

    public static void bindPoint(Context context, WindowManager windowManager,
                                 PointEvent pointEvent, int touchPointSize,
                                 int screenWidth, int screenHeight,
                                 OnPositionChangedCallback callback,int order,float initialTextSize) {

        TextView pointView = createTouchPointView(context, String.valueOf(order));
        Log.d("bind","id: " + order);
        WindowManager.LayoutParams params = createTouchPointParams(context, touchPointSize);

        params.x = pointEvent.getX();
        params.y = pointEvent.getY();

        pointEvent.setView(pointView);
        pointEvent.setLayoutParams(params);

        pointView.setTextSize(TypedValue.COMPLEX_UNIT_SP, initialTextSize);

        DraggableHelper.makeDraggable(pointView, params, windowManager,
                screenWidth, screenHeight,
                (x, y) -> {
                    pointEvent.setX(x);
                    pointEvent.setY(y);
                    if (callback != null) callback.onPositionChanged(x, y);
                });

        windowManager.addView(pointView, params);
    }

    public static void bindSlide(Context context, WindowManager windowManager,
                                 SlideEvent slideEvent, int touchPointSize,
                                 int screenWidth, int screenHeight,
                                 OnPositionChangedCallback callback,int order,float initialTextSize) {

        PointEvent start = slideEvent.getStartPoint();
        PointEvent end = slideEvent.getEndPoint();

        TextView startView = createTouchPointView(context, String.valueOf(order));
        TextView endView = createTouchPointView(context, String.valueOf(order));
        WindowManager.LayoutParams startParams = createTouchPointParams(context, touchPointSize);
        WindowManager.LayoutParams endParams = createTouchPointParams(context, touchPointSize);

        startParams.x = start.getX();
        startParams.y = start.getY();
        endParams.x = end.getX();
        endParams.y = end.getY();

        start.setView(startView);
        start.setLayoutParams(startParams);
        end.setView(endView);
        end.setLayoutParams(endParams);

        startView.setTextSize(TypedValue.COMPLEX_UNIT_SP, initialTextSize);
        endView.setTextSize(TypedValue.COMPLEX_UNIT_SP, initialTextSize);

        ConnectingLineView lineView = new ConnectingLineView(context);
        WindowManager.LayoutParams lineParams = createLineViewParams(context);

        slideEvent.setLineView(lineView);
        slideEvent.setLineParams(lineParams);

        // 拖动起点
        DraggableHelper.makeDraggable(startView, startParams, windowManager,
                screenWidth, screenHeight,
                (x, y) -> {
                    start.setX(x);
                    start.setY(y);
                    updateLinePosition(slideEvent, touchPointSize);
                    if (callback != null) callback.onPositionChanged(x, y);
                });

        // 拖动终点
        DraggableHelper.makeDraggable(endView, endParams, windowManager,
                screenWidth, screenHeight,
                (x, y) -> {
                    end.setX(x);
                    end.setY(y);
                    updateLinePosition(slideEvent, touchPointSize);
                    if (callback != null) callback.onPositionChanged(x, y);
                });

        windowManager.addView(lineView, lineParams);
        windowManager.addView(startView, startParams);
        windowManager.addView(endView, endParams);

        updateLinePosition(slideEvent, touchPointSize);
    }

    public static TextView createTouchPointView(Context context, String label) {
        TextView touchPointView = new TextView(context);
        touchPointView.setBackgroundResource(R.drawable.ring_has_bg);
        touchPointView.setGravity(Gravity.CENTER);
        touchPointView.setText(label);
        touchPointView.setTextColor(Color.WHITE);
        return touchPointView;
    }

    private static WindowManager.LayoutParams createTouchPointParams(Context context, int size) {
        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(
                size, size,
                overlayType,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT
        );
        params.gravity = Gravity.TOP | Gravity.START;
        return params;
    }

    private static WindowManager.LayoutParams createLineViewParams(Context context) {
        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.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                overlayType,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                PixelFormat.TRANSLUCENT
        );
        params.gravity = Gravity.TOP | Gravity.START;
        params.x = 0;
        params.y = 0;
        return params;
    }

    public static void updateLinePosition(SlideEvent slideEvent, int touchPointSize) {
        PointEvent start = slideEvent.getStartPoint();
        PointEvent end = slideEvent.getEndPoint();
        ConnectingLineView lineView = slideEvent.getLineView();

        if (lineView == null) return;

        float startCenterX = start.getX() + (float) touchPointSize / 2;
        float startCenterY = start.getY() + (float) touchPointSize / 2;
        float endCenterX = end.getX() + (float) touchPointSize / 2;
        float endCenterY = end.getY() + (float) touchPointSize / 2;

        lineView.setPoints(startCenterX, startCenterY, endCenterX, endCenterY);
    }
}

