package utils

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
)

// LogError Exit program with a HTTP Internal Error status code and a message (dump and die)
func LogError(str ...string) {
	//fmt.Print("Status: 500 Internal server error\r\nContent-Type: text/html; charset=utf-8\r\n\r\n")
	fmt.Print("Status: 500 Internal server error\r\nContent-Type: text/html; charset=utf-8\r\n\r\n")
	fmt.Print(strings.Join(str, ", "))
	os.Exit(0)
}

// Unauthorised Exit program with a HTTP Unauthorized status code and a message (dump and die)
// 重定向到NAS的主页
func Unauthorised() {
	fmt.Print("Status: 401 Unauthorized\r\nContent-Type: text/html; charset=utf-8\r\n\r\n")
	os.Exit(0)
}

// RespData 响应数据
func RespData(data any) {
	fmt.Print("Status: 200 OK\r\nContent-Type: application/json; charset=utf-8\r\n\r\n")
	r, err := json.Marshal(data)
	if err == nil {
		fmt.Print(string(r))
	} else {
		fmt.Print("marshal fail")
		LogError(err.Error())
	}
	os.Exit(0)
}
