package matrixbot

import (
	"encoding/base64"
	"encoding/json"
	"log"

	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/format"
	"maunium.net/go/mautrix/id"
	"roese.dev/kra-jenkins-matrixbot-message/modules/config"
)

type Representation struct {
	Hour           string `json:"hour"`
	Class          string `json:"class"`
	Representative string `json:"representative"`
	Teacher        string `json:"teacher"`
	Subject        string `json:"subject"`
	Room           string `json:"room"`
	Note           string `json:"note"`
}

func SendMessage(today string, nextDay string) {
	cfg := config.Cfg.Bot

	client, err := mautrix.NewClient(cfg.HomeserverUrl, "", "")

	if err != nil {
		log.Fatalln(err)
	}

	_, err = client.Login(&mautrix.ReqLogin{
		Type:             "m.login.password",
		Identifier:       mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: cfg.User},
		Password:         cfg.Password,
		StoreCredentials: true,
	})

	if err != nil {
		log.Fatalln(err)
	}

	var todayRepresentation []Representation
	var nextDayRepresentation []Representation

	log.Println(today)

	if today != "-" {
		representation, err := Base64DecodeAndUnmarshal(today)

		if err != nil {
			log.Fatalln(err)
		}

		todayRepresentation = representation

		log.Println(representation)
	}

	if nextDay != "-" {
		representation, err := Base64DecodeAndUnmarshal(nextDay)

		if err != nil {
			log.Fatalln(err)
		}

		nextDayRepresentation = representation
	}

	log.Println(nextDayRepresentation, todayRepresentation)

	if len(todayRepresentation) == 0 && len(nextDayRepresentation) == 0 {
		return
	}

	var msg string

	if len(todayRepresentation) > 0 {
		msg += "<b>Vertretung - Heute</b>"

		for _, entity := range todayRepresentation {
			msg += "<br>Stunde: <b>" + entity.Hour + "</b>"
			msg += "<br>Klasse: <b>" + entity.Class + "</b>"
			msg += "<br>Vertreter: <b>" + entity.Representative + "</b>"
			msg += "<br>Lehrer: <b>" + entity.Teacher + "</b>"
			msg += "<br>Fach: <b>" + entity.Subject + "</b>"
			msg += "<br>Raum: <b>" + entity.Room + "</b>"
			msg += "<br>Hinweis: <b>" + entity.Note + "</b>"
			msg += "<br>"
		}
	}

	if len(nextDayRepresentation) > 0 {
		msg += "<br><b>Vertretung - Nächster Tag</b>"

		for _, entity := range nextDayRepresentation {
			msg += "<br>Stunde: <b>" + entity.Hour + "</b>"
			msg += "<br>Klasse: <b>" + entity.Class + "</b>"
			msg += "<br>Vertreter: <b>" + entity.Representative + "</b>"
			msg += "<br>Lehrer: <b>" + entity.Teacher + "</b>"
			msg += "<br>Fach: <b>" + entity.Subject + "</b>"
			msg += "<br>Raum: <b>" + entity.Room + "</b>"
			msg += "<br>Hinweis: <b>" + entity.Note + "</b>"
			msg += "<br>"
		}
	}

	botmsg := format.RenderMarkdown(msg, false, true)

	client.SendMessageEvent(id.RoomID(cfg.RoomId), event.EventMessage, &botmsg)
}

func Base64DecodeAndUnmarshal(base64Encoded string) (representation []Representation, err error) {
	decode, err := base64.StdEncoding.DecodeString(base64Encoded)

	var rep []Representation

	if err != nil {
		return rep, err
	}

	err = json.Unmarshal(decode, &rep)

	if err != nil {
		return rep, err
	}

	return rep, nil
}
