package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func http_server(port string) {

	mux := http.NewServeMux()
	server := http.Server{
		Addr:         fmt.Sprintf(":%v", port),
		ReadTimeout:  time.Minute,
		WriteTimeout: time.Minute,
		Handler:      mux,
	}

	go func() {
		ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
		defer stop()
		<-ctx.Done() // Waits for signal
		server.Shutdown(context.Background())
	}()

	mux.HandleFunc("GET /dx2w/registers", GetRegisters())
	mux.HandleFunc("/", CatchAll())

	log.Println("http_server started, port:", port)
	defer log.Println("http_server stopped")

	if err := server.ListenAndServe(); err != http.ErrServerClosed {
		log.Println("[dxw2_logger_http_server]", err)
	}
}

func CatchAll() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/dx2w/registers", http.StatusFound)
	}
}

func GetRegisters() http.HandlerFunc {
	jsonBytes := func(data interface{}) []byte {
		json, err := json.MarshalIndent(data, "", "    ")
		if err != nil {
			return []byte(err.Error())
		}
		return json
	}
	return func(w http.ResponseWriter, r *http.Request) {
		// use lock just to get reference to register_map
		global_mutex.Lock()
		ref := register_map
		global_mutex.Unlock()
		w.Write(jsonBytes(ref))
	}
}
