package de.winniepat.citrus.gui;

import de.winniepat.citrus.managers.FriendManager;
import de.winniepat.citrus.managers.FriendManager.Friend;
import de.winniepat.citrus.managers.FriendManager.FriendRequest;
import de.winniepat.citrus.managers.WallpaperManager;
import de.winniepat.citrus.managers.ConfigManager;
import de.winniepat.citrus.utils.texture.CdnTextureDrawable;
import de.winniepat.citrus.utils.toasts.CustomToast;
import icyllis.modernui.animation.*;
import icyllis.modernui.core.Context;
import icyllis.modernui.fragment.Fragment;
import icyllis.modernui.graphics.drawable.*;
import icyllis.modernui.mc.*;
import icyllis.modernui.text.InputFilter;
import icyllis.modernui.text.TextPaint;
import icyllis.modernui.util.DataSet;
import icyllis.modernui.view.*;
import icyllis.modernui.widget.*;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.*;

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

public class FriendsFragment extends Fragment implements ScreenCallback {

    private LinearLayout mFriendsListContainer;
    private LinearLayout mRequestsListContainer;
    private EditText mAddFriendInput;
    private CdnTextureDrawable mBackground1Drawable;
    private final List<View> mStaggeredViews = new ArrayList<>();

    private float getGuiScale() {
        return (float) MinecraftClient.getInstance().getWindow().getScaleFactor();
    }

    private void setFixedTextSize(TextView tv, float px) {
        float scale = getGuiScale();
        tv.setTextSize(px / scale);
    }

    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable DataSet savedInstanceState) {
        Context context = requireContext();

        FrameLayout root = new FrameLayout(context);
        root.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));

        FrameLayout mBackgroundView = new FrameLayout(context);
        mBackgroundView.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        mBackgroundView.setScaleX(1.1f);
        mBackgroundView.setScaleY(1.1f);

        View mBackgroundView1 = new View(context);
        mBackgroundView1.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        mBackgroundView.addView(mBackgroundView1);

        root.addView(mBackgroundView);

        mBackground1Drawable = new CdnTextureDrawable();
        mBackgroundView1.setBackground(mBackground1Drawable);

        String currentWallpaper = ConfigManager.config.mainmenuWallpaper;
        if (currentWallpaper == null || currentWallpaper.isEmpty()) {
            currentWallpaper = "default.png";
        }
        WallpaperManager.loadWallpaperWithData(currentWallpaper, imageData -> {
            MuiModApi.postToUiThread(() -> mBackground1Drawable.setImageData(imageData));
        });

        ObjectAnimator bgPulseX = ObjectAnimator.ofFloat(mBackgroundView, View.SCALE_X, 1.1f, 1.15f);
        ObjectAnimator bgPulseY = ObjectAnimator.ofFloat(mBackgroundView, View.SCALE_Y, 1.1f, 1.15f);
        bgPulseX.setDuration(15000);
        bgPulseY.setDuration(15000);
        bgPulseX.setRepeatCount(ValueAnimator.INFINITE);
        bgPulseY.setRepeatCount(ValueAnimator.INFINITE);
        bgPulseX.setRepeatMode(ValueAnimator.REVERSE);
        bgPulseY.setRepeatMode(ValueAnimator.REVERSE);
        bgPulseX.start();
        bgPulseY.start();

        FrameLayout contentWrapper = new FrameLayout(context);
        contentWrapper.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        root.addView(contentWrapper);

        LinearLayout leftPanel = new LinearLayout(context);
        leftPanel.setOrientation(LinearLayout.VERTICAL);
        FrameLayout.LayoutParams leftPanelLp = new FrameLayout.LayoutParams(
                300,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        leftPanelLp.gravity = Gravity.CENTER_VERTICAL;
        leftPanelLp.setMargins(60, 0, 0, 0);
        leftPanel.setLayoutParams(leftPanelLp);

        TextView title = new TextView(context);
        title.setText("FRIENDS");
        setFixedTextSize(title, 96);
        title.getPaint().setTextStyle(TextPaint.BOLD);
        title.setTextColor(0xFFFFFFFF);
        leftPanel.addView(title);
        mStaggeredViews.add(title);

        TextView subtitle = new TextView(context);
        subtitle.setText("Manage your friends list");
        setFixedTextSize(subtitle, 32);
        subtitle.setTextColor(0xCCFFFFFF);
        LinearLayout.LayoutParams subtitleLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        subtitleLp.setMargins(0, 0, 0, dp(30));
        subtitle.setLayoutParams(subtitleLp);
        leftPanel.addView(subtitle);
        mStaggeredViews.add(subtitle);

        LinearLayout addSection = new LinearLayout(context);
        addSection.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams addSectionLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        addSectionLp.setMargins(0, 0, 0, dp(20));
        addSection.setLayoutParams(addSectionLp);

        TextView addLabel = new TextView(context);
        addLabel.setText("Add a Friend");
        setFixedTextSize(addLabel, 28);
        addLabel.setTextColor(0x88FFFFFF);
        addLabel.setPadding(0, 0, 0, dp(8));
        addSection.addView(addLabel);

        mAddFriendInput = new EditText(context);
        mAddFriendInput.setHint("Enter player name...");
        setFixedTextSize(mAddFriendInput, 32);
        mAddFriendInput.setTextColor(0xFFFFFFFF);
        mAddFriendInput.setHintTextColor(0x66FFFFFF);
        mAddFriendInput.setPadding(dp(16), dp(12), dp(16), dp(12));
        mAddFriendInput.setSingleLine(true);
        mAddFriendInput.setFilters(new InputFilter.LengthFilter(16));
        ShapeDrawable inputBg = new ShapeDrawable();
        inputBg.setShape(ShapeDrawable.RECTANGLE);
        inputBg.setColor(0x26FFFFFF);
        inputBg.setCornerRadius(dp(10));
        mAddFriendInput.setBackground(inputBg);
        LinearLayout.LayoutParams inputLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        inputLp.setMargins(0, 0, 0, dp(8));
        mAddFriendInput.setLayoutParams(inputLp);
        addSection.addView(mAddFriendInput);

        leftPanel.addView(addSection);
        mStaggeredViews.add(addSection);

        addNavButton(leftPanel, "Send Request", "multiplayer_icon", v -> sendFriendRequest());
        addNavButton(leftPanel, "Refresh", "settings_icon", v -> refreshData());
        addNavButton(leftPanel, "Back to Menu", "door_icon", v -> {
            MinecraftClient.getInstance().execute(() -> {
                MinecraftClient.getInstance().setScreen(MuiModApi.get().createScreen(new MainMenuScreen(), null, null));
            });
        });

        contentWrapper.addView(leftPanel);

        LinearLayout rightPanel = new LinearLayout(context);
        rightPanel.setOrientation(LinearLayout.HORIZONTAL);
        rightPanel.setGravity(Gravity.TOP);
        FrameLayout.LayoutParams rightPanelLp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        rightPanelLp.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        rightPanelLp.setMargins(0, 0, 60, 0);
        rightPanel.setLayoutParams(rightPanelLp);

        LinearLayout friendsPanel = createListPanel(context, "FRIENDS", true);
        LinearLayout.LayoutParams friendsPanelLp = new LinearLayout.LayoutParams(
                320,
                450
        );
        friendsPanelLp.setMargins(0, 0, dp(20), 0);
        friendsPanel.setLayoutParams(friendsPanelLp);
        rightPanel.addView(friendsPanel);
        mStaggeredViews.add(friendsPanel);

        LinearLayout requestsPanel = createListPanel(context, "REQUESTS", false);
        LinearLayout.LayoutParams requestsPanelLp = new LinearLayout.LayoutParams(
                320,
                450
        );
        requestsPanel.setLayoutParams(requestsPanelLp);
        rightPanel.addView(requestsPanel);
        mStaggeredViews.add(requestsPanel);

        contentWrapper.addView(rightPanel);

        TextView infoText = new TextView(context);
        infoText.setText("Add your friends and see what they are up to • Online status updates every minute");
        setFixedTextSize(infoText, 24);
        infoText.setTextColor(0x66FFFFFF);
        FrameLayout.LayoutParams infoLp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        infoLp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        infoLp.setMargins(0, 0, 0, dp(20));
        infoText.setLayoutParams(infoLp);
        contentWrapper.addView(infoText);
        mStaggeredViews.add(infoText);

        refreshLists();
        refreshData();
        animateEntrance();

        return root;
    }

    private LinearLayout createListPanel(Context context, String title, boolean isFriendsList) {
        LinearLayout panel = new LinearLayout(context);
        panel.setOrientation(LinearLayout.VERTICAL);
        panel.setPadding(dp(16), dp(16), dp(16), dp(16));

        ShapeDrawable panelBg = new ShapeDrawable();
        panelBg.setShape(ShapeDrawable.RECTANGLE);
        panelBg.setColor(0x33000000);
        panelBg.setCornerRadius(dp(16));
        panel.setBackground(panelBg);

        TextView titleTv = new TextView(context);
        titleTv.setText(title);
        setFixedTextSize(titleTv, 48);
        titleTv.getPaint().setTextStyle(TextPaint.BOLD);
        titleTv.setTextColor(isFriendsList ? 0xFF55FF55 : 0xFFFFD700);
        titleTv.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams titleLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        titleLp.setMargins(0, 0, 0, dp(12));
        titleTv.setLayoutParams(titleLp);
        panel.addView(titleTv);

        ScrollView scrollView = new ScrollView(context);
        LinearLayout.LayoutParams scrollLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                0, 1f
        );
        scrollView.setLayoutParams(scrollLp);
        scrollView.setScrollBarSize(dp(6));

        LinearLayout listContainer = new LinearLayout(context);
        listContainer.setOrientation(LinearLayout.VERTICAL);

        if (isFriendsList) {
            mFriendsListContainer = listContainer;
        } else {
            mRequestsListContainer = listContainer;
        }

        scrollView.addView(listContainer);
        panel.addView(scrollView);

        return panel;
    }

    private void addNavButton(LinearLayout parent, String text, String iconPath, View.OnClickListener listener) {
        Context context = requireContext();

        FrameLayout btnWrapper = new FrameLayout(context);
        LinearLayout.LayoutParams wrapperLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                56
        );
        wrapperLp.setMargins(0, 0, 0, 8);
        btnWrapper.setLayoutParams(wrapperLp);

        ShapeDrawable btnBg = new ShapeDrawable();
        btnBg.setShape(ShapeDrawable.RECTANGLE);
        btnBg.setColor(0x1AFFFFFF);
        btnBg.setCornerRadius(12);
        btnWrapper.setBackground(btnBg);

        LinearLayout content = new LinearLayout(context);
        content.setOrientation(LinearLayout.HORIZONTAL);
        content.setGravity(Gravity.CENTER_VERTICAL);
        content.setPadding(20, 0, 20, 0);

        ImageDrawable icon = new ImageDrawable("citrus", "startscreen/" + iconPath + ".png");
        icon.setBounds(0, 0, 24, 24);

        TextView tv = new TextView(context);
        tv.setText(text);
        setFixedTextSize(tv, 36);
        tv.setTextColor(0xFFFFFFFF);
        tv.setCompoundDrawables(icon, null, null, null);
        tv.setCompoundDrawablePadding(16);

        content.addView(tv);
        btnWrapper.addView(content);

        btnWrapper.setClickable(true);
        btnWrapper.setFocusable(true);
        btnWrapper.setOnClickListener(listener);

        btnWrapper.setOnHoverListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(v, View.TRANSLATION_X, 0f, (float) dp(15));
                animator.setDuration(250);
                animator.setInterpolator(TimeInterpolator.DECELERATE);
                animator.start();
                btnBg.setColor(0x33FFFFFF);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(v, View.TRANSLATION_X, (float) dp(15), 0f);
                animator.setDuration(250);
                animator.setInterpolator(TimeInterpolator.DECELERATE);
                animator.start();
                btnBg.setColor(0x1AFFFFFF);
                return true;
            }
            return false;
        });

        parent.addView(btnWrapper);
        mStaggeredViews.add(btnWrapper);
    }

    private void animateEntrance() {
        long delay = 100;
        for (View v : mStaggeredViews) {
            v.setAlpha(0);
            float startX = -dp(40);

            ViewGroup.LayoutParams lp = v.getLayoutParams();
            if (lp instanceof FrameLayout.LayoutParams flp) {
                if ((flp.gravity & Gravity.RIGHT) == Gravity.RIGHT) {
                    startX = dp(40);
                }
            }

            v.setTranslationX(startX);

            ObjectAnimator alpha = ObjectAnimator.ofFloat(v, View.ALPHA, 0f, 1f);
            ObjectAnimator trans = ObjectAnimator.ofFloat(v, View.TRANSLATION_X, startX, 0f);

            AnimatorSet set = new AnimatorSet();
            set.playTogether(alpha, trans);
            set.setDuration(800);
            set.setStartDelay(delay);
            set.setInterpolator(TimeInterpolator.DECELERATE);
            set.start();

            delay += 60;
        }
    }

    private void sendFriendRequest() {
        String name = mAddFriendInput.getText().toString().trim();
        if (name.isEmpty()) {
            CustomToast.sendCustomToast("Error", 0xFFFF5555, "Please enter a player name", 0xFFFFFFFF);
            return;
        }

        FriendManager.sendRequest(name).thenAccept(error -> {
            MuiModApi.postToUiThread(() -> {
                if (error == null) {
                    CustomToast.sendCustomToast("Success", 0xFF55FF55, "Friend request sent to " + name, 0xFFBBBBBB);
                    mAddFriendInput.setText("");
                } else {
                    CustomToast.sendCustomToast("Error", 0xFFFF5555, error, 0xFFFFFFFF);
                }
            });
        });
    }

    private void refreshData() {
        FriendManager.refresh().thenAccept(v -> {
            MuiModApi.postToUiThread(this::refreshLists);
        });
    }

    private void refreshLists() {
        if (mFriendsListContainer == null || mRequestsListContainer == null) return;

        Context context = requireContext();

        mFriendsListContainer.removeAllViews();
        List<Friend> friends = FriendManager.getFriends();

        if (friends.isEmpty()) {
            mFriendsListContainer.addView(createEmptyState(context, "No friends yet", "Add someone using the input on the left!"));
        } else {
            for (Friend friend : friends) {
                mFriendsListContainer.addView(createFriendCard(context, friend));
            }
        }

        mRequestsListContainer.removeAllViews();
        List<FriendRequest> requests = FriendManager.getIncomingRequests();

        if (requests.isEmpty()) {
            mRequestsListContainer.addView(createEmptyState(context, "No requests", "Pending requests will appear here"));
        } else {
            for (FriendRequest request : requests) {
                mRequestsListContainer.addView(createRequestCard(context, request));
            }
        }
    }

    private View createEmptyState(Context context, String title, String subtitle) {
        LinearLayout container = new LinearLayout(context);
        container.setOrientation(LinearLayout.VERTICAL);
        container.setGravity(Gravity.CENTER);
        container.setPadding(dp(16), dp(40), dp(16), dp(40));

        TextView titleTv = new TextView(context);
        titleTv.setText(title);
        setFixedTextSize(titleTv, 32);
        titleTv.setTextColor(0x88FFFFFF);
        titleTv.setGravity(Gravity.CENTER);
        container.addView(titleTv);

        TextView subtitleTv = new TextView(context);
        subtitleTv.setText(subtitle);
        setFixedTextSize(subtitleTv, 24);
        subtitleTv.setTextColor(0x55FFFFFF);
        subtitleTv.setGravity(Gravity.CENTER);
        subtitleTv.setPadding(0, dp(4), 0, 0);
        container.addView(subtitleTv);

        return container;
    }

    private View createFriendCard(Context context, Friend friend) {
        LinearLayout card = new LinearLayout(context);
        card.setOrientation(LinearLayout.VERTICAL);
        card.setPadding(dp(12), dp(10), dp(12), dp(10));
        LinearLayout.LayoutParams cardLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        cardLp.setMargins(0, 0, 0, dp(8));
        card.setLayoutParams(cardLp);

        ShapeDrawable cardBg = new ShapeDrawable();
        cardBg.setShape(ShapeDrawable.RECTANGLE);
        cardBg.setColor(0x20FFFFFF);
        cardBg.setCornerRadius(dp(10));
        card.setBackground(cardBg);

        LinearLayout topRow = new LinearLayout(context);
        topRow.setOrientation(LinearLayout.HORIZONTAL);
        topRow.setGravity(Gravity.CENTER_VERTICAL);

        View statusDot = new View(context);
        LinearLayout.LayoutParams dotLp = new LinearLayout.LayoutParams(dp(8), dp(8));
        dotLp.setMargins(0, 0, dp(8), 0);
        statusDot.setLayoutParams(dotLp);
        ShapeDrawable dotBg = new ShapeDrawable();
        dotBg.setShape(ShapeDrawable.CIRCLE);
        dotBg.setColor(friend.isOnline() ? 0xFF55FF55 : 0xFF666666);
        statusDot.setBackground(dotBg);
        topRow.addView(statusDot);

        String name = friend.player_name != null ? friend.player_name : "Unknown";
        TextView nameTv = new TextView(context);
        nameTv.setText(name);
        setFixedTextSize(nameTv, 30);
        nameTv.setTextColor(0xFFFFFFFF);
        LinearLayout.LayoutParams nameLp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
        nameTv.setLayoutParams(nameLp);
        topRow.addView(nameTv);

        TextView statusTv = new TextView(context);
        statusTv.setText(friend.isOnline() ? "Online" : "Offline");
        setFixedTextSize(statusTv, 22);
        statusTv.setTextColor(friend.isOnline() ? 0xFF55FF55 : 0x88FFFFFF);
        topRow.addView(statusTv);

        card.addView(topRow);

        String serverDisplay = friend.getServerDisplay();
        if (serverDisplay != null) {
            LinearLayout serverRow = new LinearLayout(context);
            serverRow.setOrientation(LinearLayout.HORIZONTAL);
            serverRow.setGravity(Gravity.CENTER_VERTICAL);
            LinearLayout.LayoutParams serverRowLp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            serverRowLp.setMargins(0, dp(6), 0, 0);
            serverRow.setLayoutParams(serverRowLp);

            View serverIcon = new View(context);
            LinearLayout.LayoutParams iconLp = new LinearLayout.LayoutParams(dp(6), dp(20));
            iconLp.setMargins(0, 0, dp(10), 0);
            serverIcon.setLayoutParams(iconLp);
            ShapeDrawable iconBg = new ShapeDrawable();
            iconBg.setShape(ShapeDrawable.RECTANGLE);
            iconBg.setColor(0xFF4a90d9);
            iconBg.setCornerRadius(dp(3));
            serverIcon.setBackground(iconBg);
            serverRow.addView(serverIcon);

            TextView serverTv = new TextView(context);
            serverTv.setText("Playing on: " + serverDisplay);
            setFixedTextSize(serverTv, 28);
            serverTv.setTextColor(0xFF4a90d9);
            LinearLayout.LayoutParams serverTvLp = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
            serverTv.setLayoutParams(serverTvLp);
            serverRow.addView(serverTv);

            TextView joinBtn = new TextView(context);
            joinBtn.setText("Copy IP");
            setFixedTextSize(joinBtn, 24);
            joinBtn.setTextColor(0xFFFFFFFF);
            joinBtn.setPadding(dp(12), dp(6), dp(12), dp(6));
            ShapeDrawable joinBg = new ShapeDrawable();
            joinBg.setShape(ShapeDrawable.RECTANGLE);
            joinBg.setColor(0xFF4a90d9);
            joinBg.setCornerRadius(dp(6));
            joinBtn.setBackground(joinBg);
            joinBtn.setClickable(true);
            String finalServerDisplay = serverDisplay;
            joinBtn.setOnClickListener(v -> {
                MinecraftClient.getInstance().keyboard.setClipboard(finalServerDisplay);
                CustomToast.sendCustomToast("Copied!", 0xFF4a90d9, "Server IP copied to clipboard", 0xFFBBBBBB);
            });
            joinBtn.setOnHoverListener((v, event) -> {
                if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
                    joinBg.setColor(0xFF6ab0f9);
                } else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
                    joinBg.setColor(0xFF4a90d9);
                }
                return false;
            });
            serverRow.addView(joinBtn);

            card.addView(serverRow);
        }

        LinearLayout bottomRow = new LinearLayout(context);
        bottomRow.setOrientation(LinearLayout.HORIZONTAL);
        bottomRow.setGravity(Gravity.RIGHT);
        LinearLayout.LayoutParams bottomRowLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        bottomRowLp.setMargins(0, dp(8), 0, 0);
        bottomRow.setLayoutParams(bottomRowLp);

        TextView removeBtn = createSmallActionButton(context, "Remove", 0xFFd9534f);
        removeBtn.setOnClickListener(v -> {
            FriendManager.removeFriend(friend.getUuid()).thenAccept(error -> {
                MuiModApi.postToUiThread(() -> {
                    if (error == null) {
                        refreshLists();
                        CustomToast.sendCustomToast("Removed", 0xFFFF9955, name + " was removed from friends", 0xFFBBBBBB);
                    } else {
                        CustomToast.sendCustomToast("Error", 0xFFFF5555, error, 0xFFFFFFFF);
                    }
                });
            });
        });
        bottomRow.addView(removeBtn);

        card.addView(bottomRow);

        return card;
    }

    private View createRequestCard(Context context, FriendRequest request) {
        LinearLayout card = new LinearLayout(context);
        card.setOrientation(LinearLayout.VERTICAL);
        card.setPadding(dp(12), dp(10), dp(12), dp(10));
        LinearLayout.LayoutParams cardLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        cardLp.setMargins(0, 0, 0, dp(8));
        card.setLayoutParams(cardLp);

        ShapeDrawable cardBg = new ShapeDrawable();
        cardBg.setShape(ShapeDrawable.RECTANGLE);
        cardBg.setColor(0x20FFFFFF);
        cardBg.setCornerRadius(dp(10));
        card.setBackground(cardBg);

        View accentBar = new View(context);
        LinearLayout.LayoutParams accentLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(3));
        accentLp.setMargins(0, 0, 0, dp(8));
        accentBar.setLayoutParams(accentLp);
        ShapeDrawable accentBg = new ShapeDrawable();
        accentBg.setShape(ShapeDrawable.RECTANGLE);
        accentBg.setColor(0xFFFFD700);
        accentBg.setCornerRadius(dp(2));
        accentBar.setBackground(accentBg);
        card.addView(accentBar);

        String name = request.player_name != null ? request.player_name : "Unknown";
        TextView nameTv = new TextView(context);
        nameTv.setText(name);
        setFixedTextSize(nameTv, 30);
        nameTv.setTextColor(0xFFFFFFFF);
        card.addView(nameTv);

        TextView labelTv = new TextView(context);
        labelTv.setText("wants to be your friend");
        setFixedTextSize(labelTv, 22);
        labelTv.setTextColor(0x88FFFFFF);
        card.addView(labelTv);

        LinearLayout actionsRow = new LinearLayout(context);
        actionsRow.setOrientation(LinearLayout.HORIZONTAL);
        actionsRow.setGravity(Gravity.RIGHT);
        LinearLayout.LayoutParams actionsLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        actionsLp.setMargins(0, dp(10), 0, 0);
        actionsRow.setLayoutParams(actionsLp);

        TextView declineBtn = createSmallActionButton(context, "Decline", 0xFFd9534f);
        declineBtn.setOnClickListener(v -> {
            FriendManager.removeFriend(request.getUuid()).thenAccept(error -> {
                MuiModApi.postToUiThread(() -> {
                    if (error == null) {
                        refreshLists();
                    } else {
                        CustomToast.sendCustomToast("Error", 0xFFFF5555, error, 0xFFFFFFFF);
                    }
                });
            });
        });
        LinearLayout.LayoutParams declineLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        declineLp.setMargins(0, 0, dp(8), 0);
        declineBtn.setLayoutParams(declineLp);
        actionsRow.addView(declineBtn);

        TextView acceptBtn = createSmallActionButton(context, "Accept", 0xFF5cb85c);
        acceptBtn.setOnClickListener(v -> {
            FriendManager.acceptRequest(request.getUuid()).thenAccept(error -> {
                MuiModApi.postToUiThread(() -> {
                    if (error == null) {
                        refreshLists();
                        CustomToast.sendCustomToast("Success", 0xFF55FF55, "You are now friends with " + name, 0xFFBBBBBB);
                    } else {
                        CustomToast.sendCustomToast("Error", 0xFFFF5555, error, 0xFFFFFFFF);
                    }
                });
            });
        });
        actionsRow.addView(acceptBtn);

        card.addView(actionsRow);

        return card;
    }

    private TextView createSmallActionButton(Context context, String text, int color) {
        TextView btn = new TextView(context);
        btn.setText(text);
        setFixedTextSize(btn, 24);
        btn.setTextColor(0xFFFFFFFF);
        btn.setPadding(dp(14), dp(6), dp(14), dp(6));
        btn.setGravity(Gravity.CENTER);

        ShapeDrawable bg = new ShapeDrawable();
        bg.setShape(ShapeDrawable.RECTANGLE);
        bg.setColor(color);
        bg.setCornerRadius(dp(6));
        btn.setBackground(bg);
        btn.setClickable(true);

        btn.setOnHoverListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
                bg.setColor(lightenColor(color, 0.2f));
            } else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
                bg.setColor(color);
            }
            return false;
        });

        return btn;
    }

    private int lightenColor(int color, float factor) {
        int a = (color >> 24) & 0xFF;
        int r = Math.min(255, (int) (((color >> 16) & 0xFF) * (1 + factor)));
        int g = Math.min(255, (int) (((color >> 8) & 0xFF) * (1 + factor)));
        int b = Math.min(255, (int) ((color & 0xFF) * (1 + factor)));
        return (a << 24) | (r << 16) | (g << 8) | b;
    }

    private int dp(int value) {
        return (int) (value * getContext().getResources().getDisplayMetrics().density);
    }

    @Override
    public boolean isPauseScreen() {
        return false;
    }

    @Override
    public boolean shouldBlurBackground() {
        return false;
    }

    @Override
    public boolean hasDefaultBackground() {
        return false;
    }
}

