package publisher

import (
	"errors"
	"fmt"
	"strings"
	"sync"

	"gordenko.dev/dima/textutil"
	"gordenko.dev/dima/ws"
)

type PrivatePublisher struct {
	mutex                      sync.Mutex
	server                     *ws.PrivateServer
	subscribers                map[string]map[*ws.Conn]bool
	connAsChannels             map[string]*ws.Conn
	subscribeRequestHandlers   map[string]func(*ws.Conn, int64, string, string)
	unsubscribeRequestHandlers map[string]func(*ws.Conn, int64, string, string)
}

type PrivatePublisherOptions struct {
	WsPrivateServer *ws.PrivateServer
}

func NewPrivatePublisher(opt PrivatePublisherOptions) (*PrivatePublisher, error) {
	if opt.WsPrivateServer == nil {
		return nil, errors.New("WsPrivateServer option is required")
	}
	s := &PrivatePublisher{
		server:                     opt.WsPrivateServer,
		subscribers:                make(map[string]map[*ws.Conn]bool),
		connAsChannels:             make(map[string]*ws.Conn),
		subscribeRequestHandlers:   make(map[string]func(*ws.Conn, int64, string, string)),
		unsubscribeRequestHandlers: make(map[string]func(*ws.Conn, int64, string, string)),
	}
	s.server.OnDisconnected(s.onDisconnected)

	s.server.Func(WsMsgSubscribe, s.onSubscribe)
	s.server.Func(WsMsgUnsubscribe, s.onUnsubscribe)
	s.server.Func(WsMsgUnsubscribeToAll, s.onUnsubscribeToAll)
	s.server.Func(WsMsgMakeConnAsChannel, s.onMakeConnAsChannel)
	return s, nil
}

func (s *PrivatePublisher) onDisconnected(conn *ws.Conn, userID int64) {
	s.onUnsubscribeToAll(conn, userID)
}

func (s *PrivatePublisher) onSubscribe(conn *ws.Conn, userID int64, topic string) {
	if strings.HasPrefix(topic, "#") {
		return
	}
	prefix, arg := parseTopic(topic)
	f, ok := s.subscribeRequestHandlers[prefix]
	if ok {
		f(conn, userID, topic, arg)
	}
}

func (s *PrivatePublisher) onUnsubscribe(conn *ws.Conn, userID int64, topic string) {
	if strings.HasPrefix(topic, "#") {
		s.mutex.Lock()
		delete(s.connAsChannels, topic)
		s.mutex.Unlock()
	} else {
		s.unsubscribeToTopic(conn, userID, topic)
	}
}

func (s *PrivatePublisher) onUnsubscribeToAll(expiredConn *ws.Conn, userID int64) {
	var (
		connAsChannels []string
		topics         []string
	)
	s.mutex.Lock()
	for channel, conn := range s.connAsChannels {
		if conn == expiredConn {
			connAsChannels = append(connAsChannels, channel)
		}
	}
	for _, channel := range connAsChannels {
		delete(s.connAsChannels, channel)
	}
	for topic, connMap := range s.subscribers {
		if _, ok := connMap[expiredConn]; ok {
			topics = append(topics, topic)
		}
	}
	s.mutex.Unlock()
	for _, topic := range topics {
		s.unsubscribeToTopic(expiredConn, userID, topic)
	}
}

func (s *PrivatePublisher) unsubscribeToTopic(conn *ws.Conn, userID int64, topic string) {
	prefix, arg := parseTopic(topic)
	f, ok := s.unsubscribeRequestHandlers[prefix]
	if ok {
		f(conn, userID, topic, arg)
	} else {
		s.UnsubscribeTo(conn, topic)
	}
}

func (s *PrivatePublisher) onMakeConnAsChannel(conn *ws.Conn, userID int64) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	for {
		channel := "#" + textutil.RandomString(textutil.Alpha52Digit, 16)
		_, ok := s.connAsChannels[channel]
		if !ok {
			s.connAsChannels[channel] = conn
			conn.Send(WsMsgConnAsChannel, channel)
			return
		}
	}
}

func (s *PrivatePublisher) SubscribeTo(conn *ws.Conn, topic string) {
	s.mutex.Lock()
	connMap, ok := s.subscribers[topic]
	if !ok {
		connMap = make(map[*ws.Conn]bool)
		s.subscribers[topic] = connMap
	}
	connMap[conn] = true
	s.mutex.Unlock()
}

func (s *PrivatePublisher) UnsubscribeTo(conn *ws.Conn, topic string) {
	s.mutex.Lock()
	connMap, ok := s.subscribers[topic]
	if ok {
		delete(connMap, conn)
		if len(connMap) == 0 {
			delete(s.subscribers, topic)
		}
	}
	s.mutex.Unlock()
}

func (s *PrivatePublisher) Publish(topic string, messageType string, message any) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	if strings.HasPrefix(topic, "#") {
		conn := s.connAsChannels[topic]
		if conn != nil {
			conn.Send(messageType, message)
		}
	} else {
		connMap, ok := s.subscribers[topic]
		if ok {
			s.server.Broadcast(messageType, message, connMap)
		}
	}
}

func (s *PrivatePublisher) HasSubscribers(topic string) bool {
	s.mutex.Lock()
	_, has := s.subscribers[topic]
	s.mutex.Unlock()
	return has
}

func (s *PrivatePublisher) OnTopicSubscribeRequest(topicPrefix string, f func(*ws.Conn, int64, string, string)) {
	if strings.Contains(topicPrefix, TopicSeparator) {
		panic(fmt.Sprintf("wrong topic prefix '%s', because of topic separator '%s'",
			topicPrefix, TopicSeparator))
	}
	s.mutex.Lock()
	s.subscribeRequestHandlers[topicPrefix] = f
	s.mutex.Unlock()
}

func (s *PrivatePublisher) OnTopicUnsubscribeRequest(topicPrefix string, f func(*ws.Conn, int64, string, string)) {
	if strings.Contains(topicPrefix, TopicSeparator) {
		panic(fmt.Sprintf("wrong topic prefix '%s', because of topic separator '%s'",
			topicPrefix, TopicSeparator))
	}
	s.mutex.Lock()
	s.unsubscribeRequestHandlers[topicPrefix] = f
	s.mutex.Unlock()
}
