package com.home4j.service;

import com.home4j.entity.Bookmark;
import com.home4j.entity.BookmarkGroup;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;

import java.util.List;

/**
 * 书签服务
 */
@ApplicationScoped
public class BookmarkService {

    /**
     * 获取所有可见分组及其书签
     */
    public List<BookmarkGroup> getAllVisibleGroupsWithBookmarks() {
        return BookmarkGroup.findAllVisibleOrdered();
    }

    /**
     * 获取所有分组（管理用）
     */
    public List<BookmarkGroup> getAllGroups() {
        return BookmarkGroup.findAllOrdered();
    }

    /**
     * 创建分组
     */
    @Transactional
    public BookmarkGroup createGroup(String name, String icon) {
        BookmarkGroup group = new BookmarkGroup();
        group.name = name;
        group.icon = icon;
        group.sortOrder = BookmarkGroup.getNextSortOrder();
        group.visible = true;
        group.persist();
        return group;
    }

    /**
     * 更新分组
     */
    @Transactional
    public BookmarkGroup updateGroup(Long id, String name, String icon, Boolean visible) {
        BookmarkGroup group = BookmarkGroup.findById(id);
        if (group != null) {
            group.name = name;
            group.icon = icon;
            if (visible != null) {
                group.visible = visible;
            }
            group.persist();
        }
        return group;
    }

    /**
     * 删除分组（级联删除书签）
     */
    @Transactional
    public boolean deleteGroup(Long id) {
        BookmarkGroup group = BookmarkGroup.findById(id);
        if (group != null) {
            group.delete();
            return true;
        }
        return false;
    }

    /**
     * 创建书签
     */
    @Transactional
    public Bookmark createBookmark(Long groupId, String name, String url, String description, String icon, Boolean openInNewTab) {
        return createBookmark(groupId, name, url, description, icon, openInNewTab, null, null);
    }

    /**
     * 创建书签（包含显示模式和背景图）
     */
    @Transactional
    public Bookmark createBookmark(Long groupId, String name, String url, String description, String icon, Boolean openInNewTab, String displayMode, String backgroundImage) {
        Bookmark bookmark = new Bookmark();
        if (groupId != null) {
            bookmark.group = BookmarkGroup.findById(groupId);
        }
        bookmark.name = name;
        bookmark.url = url;
        bookmark.description = description;
        bookmark.icon = icon;
        bookmark.openInNewTab = openInNewTab != null ? openInNewTab : true;
        bookmark.displayMode = displayMode != null ? displayMode : "icon";
        bookmark.backgroundImage = backgroundImage;
        bookmark.sortOrder = Bookmark.getNextSortOrder(groupId);
        bookmark.visible = true;
        bookmark.persist();
        return bookmark;
    }

    /**
     * 更新书签
     */
    @Transactional
    public Bookmark updateBookmark(Long id, Long groupId, String name, String url, String description, String icon, Boolean visible, Boolean openInNewTab) {
        return updateBookmark(id, groupId, name, url, description, icon, visible, openInNewTab, null, null);
    }

    /**
     * 更新书签（包含显示模式和背景图）
     */
    @Transactional
    public Bookmark updateBookmark(Long id, Long groupId, String name, String url, String description, String icon, Boolean visible, Boolean openInNewTab, String displayMode, String backgroundImage) {
        Bookmark bookmark = Bookmark.findById(id);
        if (bookmark != null) {
            if (groupId != null) {
                bookmark.group = BookmarkGroup.findById(groupId);
            } else {
                bookmark.group = null;
            }
            bookmark.name = name;
            bookmark.url = url;
            bookmark.description = description;
            bookmark.icon = icon;
            if (visible != null) {
                bookmark.visible = visible;
            }
            if (openInNewTab != null) {
                bookmark.openInNewTab = openInNewTab;
            }
            if (displayMode != null) {
                bookmark.displayMode = displayMode;
            }
            if (backgroundImage != null) {
                bookmark.backgroundImage = backgroundImage;
            }
            bookmark.persist();
        }
        return bookmark;
    }

    /**
     * 删除书签
     */
    @Transactional
    public boolean deleteBookmark(Long id) {
        Bookmark bookmark = Bookmark.findById(id);
        if (bookmark != null) {
            bookmark.delete();
            return true;
        }
        return false;
    }

    /**
     * 搜索书签
     */
    public List<Bookmark> searchBookmarks(String keyword) {
        return Bookmark.search(keyword);
    }

    /**
     * 更新分组排序
     */
    @Transactional
    public void updateGroupOrder(List<Long> groupIds) {
        for (int i = 0; i < groupIds.size(); i++) {
            BookmarkGroup group = BookmarkGroup.findById(groupIds.get(i));
            if (group != null) {
                group.sortOrder = i;
                group.persist();
            }
        }
    }

    /**
     * 更新书签排序
     */
    @Transactional
    public void updateBookmarkOrder(List<Long> bookmarkIds) {
        for (int i = 0; i < bookmarkIds.size(); i++) {
            Bookmark bookmark = Bookmark.findById(bookmarkIds.get(i));
            if (bookmark != null) {
                bookmark.sortOrder = i;
                bookmark.persist();
            }
        }
    }
}
