package service

import (
	"context"

	"kra/app/system/internal/biz"
)

type MenuService struct{ uc *biz.MenuUsecase }

func NewMenuService(uc *biz.MenuUsecase) *MenuService { return &MenuService{uc: uc} }

func (s *MenuService) List(ctx context.Context) ([]*MenuResponse, error) {
	items, err := s.uc.List(ctx)
	if err != nil {
		return nil, err
	}
	return menuResponses(items), nil
}

func (s *MenuService) Tree(ctx context.Context) ([]*MenuResponse, error) {
	items, err := s.uc.Tree(ctx)
	if err != nil {
		return nil, err
	}
	return menuResponses(items), nil
}

func (s *MenuService) Create(ctx context.Context, req *MenuRequest) error {
	return s.uc.Create(ctx, menuDomain(req))
}
func (s *MenuService) Update(ctx context.Context, req *MenuRequest) error {
	return s.uc.Update(ctx, menuDomain(req))
}
func (s *MenuService) Delete(ctx context.Context, id uint) error { return s.uc.Delete(ctx, id) }

func (s *MenuService) Find(ctx context.Context, id uint) (*MenuResponse, error) {
	item, err := s.uc.Find(ctx, id)
	if err != nil {
		return nil, err
	}
	return menuResponse(item), nil
}

func (s *MenuService) SetAuthorityMenus(ctx context.Context, req *SetAuthorityMenusRequest) error {
	ids := make([]uint, 0, len(req.Menus))
	for _, menu := range req.Menus {
		ids = append(ids, menu.ID)
	}
	return s.uc.SetAuthorityMenus(ctx, req.AuthorityID, ids)
}

func (s *MenuService) AuthorityMenus(ctx context.Context, authorityID uint) ([]*AuthorityMenuResponse, error) {
	items, err := s.uc.AuthorityMenus(ctx, authorityID)
	if err != nil {
		return nil, err
	}
	var out []*AuthorityMenuResponse
	for _, item := range items {
		item.MenuID = item.ID
		out = append(out, &AuthorityMenuResponse{DynamicMenuResponse: dynamicMenuResponse(item), AuthorityID: authorityID})
	}
	return out, nil
}

func (s *MenuService) RoleIDs(ctx context.Context, menuID uint) ([]uint, error) {
	return s.uc.RoleIDs(ctx, menuID)
}
func (s *MenuService) DefaultRouterRoleIDs(ctx context.Context, menuID uint) ([]uint, error) {
	return s.uc.DefaultRouterRoleIDs(ctx, menuID)
}
func (s *MenuService) SetRoles(ctx context.Context, req *SetMenuRolesRequest) error {
	return s.uc.SetRoles(ctx, req.MenuID, req.AuthorityIDs)
}

func menuDomain(v *MenuRequest) *biz.Menu {
	menu := &biz.Menu{CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, ID: v.ID, ParentID: v.ParentID, Path: v.Path, Name: v.Name, Hidden: v.Hidden, Component: v.Component, Sort: v.Sort, ActiveName: v.Meta.ActiveName, KeepAlive: v.Meta.KeepAlive, DefaultMenu: v.Meta.DefaultMenu, Title: v.Meta.Title, Icon: v.Meta.Icon, CloseTab: v.Meta.CloseTab, TransitionType: v.Meta.TransitionType}
	menu.Buttons = make([]*biz.MenuButton, 0, len(v.MenuButtons))
	for _, button := range v.MenuButtons {
		menu.Buttons = append(menu.Buttons, &biz.MenuButton{CreatedAt: button.CreatedAt, UpdatedAt: button.UpdatedAt, ID: button.ID, Name: button.Name, Description: button.Description, MenuID: v.ID})
	}
	menu.Parameters = make([]*biz.MenuParameter, 0, len(v.Parameters))
	for _, parameter := range v.Parameters {
		menu.Parameters = append(menu.Parameters, &biz.MenuParameter{CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, ID: parameter.ID, MenuID: v.ID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
	}
	return menu
}

func menuResponse(menu *biz.Menu) *MenuResponse {
	var children []*MenuResponse
	if menu.Children != nil {
		children = make([]*MenuResponse, 0, len(menu.Children))
		for _, child := range menu.Children {
			children = append(children, menuResponse(child))
		}
	}
	var buttons []MenuButtonResponse
	if menu.Buttons != nil {
		buttons = make([]MenuButtonResponse, 0, len(menu.Buttons))
		for _, button := range menu.Buttons {
			buttons = append(buttons, MenuButtonResponse{CreatedAt: button.CreatedAt, UpdatedAt: button.UpdatedAt, ID: button.ID, Name: button.Name, Description: button.Description, SysBaseMenuID: button.MenuID})
		}
	}
	var parameters []MenuParameterResponse
	if menu.Parameters != nil {
		parameters = make([]MenuParameterResponse, 0, len(menu.Parameters))
		for _, parameter := range menu.Parameters {
			parameters = append(parameters, MenuParameterResponse{CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, ID: parameter.ID, SysBaseMenuID: parameter.MenuID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
		}
	}
	return &MenuResponse{CreatedAt: menu.CreatedAt, UpdatedAt: menu.UpdatedAt, ID: menu.ID, MenuID: menu.MenuID, ParentID: menu.ParentID, Path: menu.Path, Name: menu.Name, Hidden: menu.Hidden, Component: menu.Component, Sort: menu.Sort, Meta: MenuMetaResponse{ActiveName: menu.ActiveName, KeepAlive: menu.KeepAlive, DefaultMenu: menu.DefaultMenu, Title: menu.Title, Icon: menu.Icon, CloseTab: menu.CloseTab, TransitionType: menu.TransitionType}, Children: children, Parameters: parameters, MenuButtons: buttons}
}

func dynamicMenuResponse(menu *biz.Menu) *DynamicMenuResponse {
	var children []*DynamicMenuResponse
	if menu.Children != nil {
		children = make([]*DynamicMenuResponse, 0, len(menu.Children))
		for _, child := range menu.Children {
			children = append(children, dynamicMenuResponse(child))
		}
	}
	var parameters []MenuParameterResponse
	if menu.Parameters != nil {
		parameters = make([]MenuParameterResponse, 0, len(menu.Parameters))
		for _, parameter := range menu.Parameters {
			parameters = append(parameters, MenuParameterResponse{CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, ID: parameter.ID, SysBaseMenuID: parameter.MenuID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
		}
	}
	var buttonAuthorities map[string]uint
	for _, button := range menu.Buttons {
		if button.AuthorityID == 0 {
			continue
		}
		if buttonAuthorities == nil {
			buttonAuthorities = make(map[string]uint)
		}
		buttonAuthorities[button.Name] = button.AuthorityID
	}
	return &DynamicMenuResponse{CreatedAt: menu.CreatedAt, UpdatedAt: menu.UpdatedAt, ID: menu.ID, MenuID: menu.MenuID, ParentID: menu.ParentID, Path: menu.Path, Name: menu.Name, Hidden: menu.Hidden, Component: menu.Component, Sort: menu.Sort, Meta: MenuMetaResponse{ActiveName: menu.ActiveName, KeepAlive: menu.KeepAlive, DefaultMenu: menu.DefaultMenu, Title: menu.Title, Icon: menu.Icon, CloseTab: menu.CloseTab, TransitionType: menu.TransitionType}, Children: children, Parameters: parameters, MenuButtons: nil, Buttons: buttonAuthorities}
}

func menuResponses(items []*biz.Menu) []*MenuResponse {
	if items == nil {
		return nil
	}
	out := make([]*MenuResponse, 0, len(items))
	for _, item := range items {
		out = append(out, menuResponse(item))
	}
	return out
}

func dynamicMenuResponses(items []*biz.Menu) []*DynamicMenuResponse {
	out := make([]*DynamicMenuResponse, 0, len(items))
	for _, item := range items {
		out = append(out, dynamicMenuResponse(item))
	}
	return out
}
