package main

import (
	"gitea.difrex.ru/Umbrella/fetcher/i2es"
	"github.com/emirpasic/gods/maps/hashmap"
	"github.com/google/uuid"
	log "github.com/sirupsen/logrus"
)

type Container struct {
	TopPosts *hashmap.Map
	Comments *hashmap.Map
	ToUpdate *hashmap.Map
}

func newContainer() Container {
	return Container{
		hashmap.New(),
		hashmap.New(),
		hashmap.New(),
	}
}

func (c *Conf) reassignTopic(con *Container) {
	totalDocs := c.getDocsCount()

	// Populate containers
	for i := 0; i < int(totalDocs); i += c.Step {
		msgs := c.GetDocs(i)
		for _, msg := range msgs.Hits.Hits {
			if msg.Source.TopicID == "" {
				m := msg.Source
				m.TopicID = uuid.New().URN()
				log.Infof("Assign new topic id `%s` for message `%s`\n", m.TopicID, m.MsgID)
				con.TopPosts.Put(m.MsgID, m)
			} else {
				m := msg.Source
				con.Comments.Put(m.MsgID, m)
			}
		}
		c.processComments(con)
		log.Info("Top posts size ", con.TopPosts.Size())
		log.Info("Comments size ", con.Comments.Size())
	}
	log.Infof("\n%+v\n", con.ToUpdate.Size())
}

func (c *Conf) processComments(con *Container) {
	for _, msg := range con.Comments.Values() {
		m := msg.(i2es.ESDoc)
		if top, ok := con.TopPosts.Get(m.Repto); ok {
			log.Info("Found topic id ", top.(i2es.ESDoc).TopicID)
			m.TopicID = top.(i2es.ESDoc).TopicID
			con.ToUpdate.Put(m.MsgID, m)
			con.Comments.Remove(m.MsgID)
			continue
		}
		if comment, ok := con.ToUpdate.Get(m.Repto); ok {
			if comment.(i2es.ESDoc).TopicID != "" {
				log.Info("Found topic id ", comment.(i2es.ESDoc).TopicID)
				m.TopicID = comment.(i2es.ESDoc).TopicID
				con.ToUpdate.Put(m.MsgID, m)
				con.Comments.Remove(m.MsgID)
			}
		}
	}
}
