package main

import (
	"encoding/base64"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"html"
	"io/ioutil"
	"log"
	"math/rand"
	"os"

	"srs.epita.fr/fic-server/libfic"
)

type askOpenHint struct {
	HintId int64 `json:"id"`
}

func treatOpeningHint(pathname string, team *fic.Team) {
	// Generate a unique identifier to follow the request in logs
	bid := make([]byte, 5)
	binary.LittleEndian.PutUint32(bid, rand.Uint32())
	id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
	log.Println(id, "New openingHint receive", pathname)

	var ask askOpenHint

	if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
		log.Printf("%s [ERR] %s\n", id, err)
	} else if err = json.Unmarshal(cnt_raw, &ask); err != nil {
		log.Printf("%s [ERR] %s\n", id, err)
	} else if ask.HintId == 0 {
		log.Printf("%s [WRN] Invalid content in hint file: %s\n", id, pathname)
		os.Remove(pathname)
	} else if hint, err := fic.GetHint(ask.HintId); err != nil {
		log.Printf("%s [ERR] Unable to retrieve the given hint: %s\n", id, err)
	} else if exercice, err := hint.GetExercice(); err != nil {
		log.Printf("%s [ERR] Unable to retrieve the hint's underlying exercice: %s\n", id, err)
	} else if exercice.Disabled {
		log.Println("[!!!] The team submits something for a disabled exercice")
	} else if !team.HasAccess(exercice) {
		log.Printf("%s [!!!] The team asks to open an hint whereas it doesn't have access to the exercice\n", id)
	} else if !team.CanSeeHint(hint) {
		log.Printf("%s [!!!] The team asks to open an hint whereas it doesn't have access to it due to hint dependencies\n", id)
	} else if err = team.OpenHint(hint); err != nil && !fic.DBIsDuplicateKeyError(err) { // Skip DUPLICATE KEY errors
		log.Printf("%s [ERR] Unable to open hint: %s\n", id, err)
	} else {
		// Write event
		if lvl, err := exercice.GetLevel(); err != nil {
			log.Printf("%s [WRN] %s\n", id, err)
		} else if theme, err := fic.GetTheme(exercice.IdTheme); err != nil {
			log.Printf("%s [WRN] %s\n", id, err)
		} else if _, err = fic.NewEvent(fmt.Sprintf("L'équipe %s a dévoilé un indice pour le <strong>%d<sup>e</sup></strong> défi %s&#160;!", html.EscapeString(team.Name), lvl, theme.Name), "info"); err != nil {
			log.Printf("%s [WRN] Unable to create event: %s\n", id, err)
		}

		appendGenQueue(fic.GenStruct{Id: id, Type: fic.GenTeam, TeamId: team.Id})
		appendGenQueue(fic.GenStruct{Id: id, Type: fic.GenEvents})
		if err = os.Remove(pathname); err != nil {
			log.Printf("%s [ERR] %s\n", id, err)
		}
	}
}
