package main

import (
	"errors"
	"fmt"
	"github.com/gorilla/mux"
	"log"
	"net/http"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"
)

const DEBUG = true
const ROOT_URL = "climate/"

func main() {
	err := setup()
	defer teardown()
	if err == nil {
		startServer()
	} else {
		fmt.Println(err)
	}
}

func setup() error {
	err := InitDb()
	if err != nil {
		return err
	}
	return nil
}

func teardown() {
	CloseDb()
}

func startServer() {
	port := "8001"
	r := mux.NewRouter()
	r.HandleFunc("/", showCharts).Methods("GET")
	r.HandleFunc("/", showCharts).Methods("GET").Queries("show-minutes", "{[0-9]+}")
	r.HandleFunc("/", saveSnapshot).Methods("POST")
	r.HandleFunc("/data", sendData).Methods("GET")
	r.HandleFunc("/data", sendData).Methods("GET").Queries("since", "")
	r.HandleFunc("/data/now", createAndSendSnapshot).Methods("GET")
	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
	http.Handle("", r)
	fmt.Printf("Listening on port %s...\n", port)
	quitSnapshot := make(chan int, 1)
	go saveSnapshotOnInterval(30, quitSnapshot)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

func showCharts(w http.ResponseWriter, r *http.Request) {
	minutes := r.FormValue("show-minutes")
	if minutes != "" {
		if _, err := strconv.ParseInt(minutes, 10, 0); err != nil {
			http.Redirect(w, r, "/" + ROOT_URL + "/", 303)
		}
	}
	http.ServeFile(w, r, "charts.html")
}

func sendData(w http.ResponseWriter, r *http.Request) {
	dateSince := time.Now()
	sinceQuery := r.FormValue("since")
	if sinceQuery != "" {
		newDateSince, err := time.Parse(time.RFC3339Nano, sinceQuery)
		if err != nil {
			http.Redirect(w, r, "/" + ROOT_URL + "/", 303)
		}
		dateSince = newDateSince
	}
	records, err := getSnapshotRecordsFromDb(dateSince)
	if internalErrorOnErr(fmt.Errorf("couldn't get snapshots from db: %w", err), w, r) { return }
	json, err := createJsonFromSnapshotRecords(records)
	if internalErrorOnErr(fmt.Errorf("couldn't create json from snapshots: %w", err), w, r) { return }
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, string(json))
}

func createAndSendSnapshot(w http.ResponseWriter, r *http.Request) {
	snapshot, err := getSnapshotFromPythonScript()
	if internalErrorOnErr(fmt.Errorf("couldn't create a snapshot: %w", err), w, r) { return }
	_, err = writeSnapshotToDb(snapshot)
	if internalErrorOnErr(fmt.Errorf("couldn't save snapshot to db: %w", err), w, r) { return }
	dbSnapshotRecord, err := getLastSnapshotRecordFromDb()
	json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{dbSnapshotRecord})
	if internalErrorOnErr(fmt.Errorf("couldn't create json from last snapshots: %w", err), w, r) { return }
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, string(json))
}

func saveSnapshot(w http.ResponseWriter, r *http.Request) {
	snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body)
	if internalErrorOnErr(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r) { return }
	newId, err := writeSnapshotToDb(snapshotSub)
	if internalErrorOnErr(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r) { return }
	fmt.Fprintf(w, "{id: %v}", newId)
}

func saveSnapshotOnInterval(seconds int64, stop chan int) {
	var err error = nil
	for {
		if err != nil {
			log.Println(err.Error())
		}
		snapshot, err := getSnapshotFromPythonScript()
		if err != nil {
			continue
		}
		_, err = writeSnapshotToDb(snapshot)
		if err != nil {
			continue
		}
		select {
		case <-stop:
			break
		default:
			time.Sleep(time.Duration(seconds) * time.Second)
		}
	}
}

func getSnapshotFromPythonScript() (*SnapshotSubmission, error) {
	process := exec.Command("climate-pinger.py")
	process.Stdout = os.Stdout
	process.Stderr = os.Stderr
	err := process.Run()
	if err != nil {
		return nil, err
	}
	output, err := process.Output()
	if err != nil {
		return nil, err
	}
	tokens := strings.Split(string(output), "\n")
	if len(tokens) != 4 {
		return nil, errors.New(fmt.Sprintf("Strange python output: %s", output))
	}
	snapshot := SnapshotSubmission{
		Timestamp: tokens[0],
		Temp:      tokens[1],
		Humidity:  tokens[2],
		Co2:       tokens[3],
	}
	return &snapshot, nil
}

func internalErrorOnErr(err error, w http.ResponseWriter, r *http.Request) bool {
	if errors.Unwrap(err) != nil {
		errorMessage := "Internal Server Error!"
		if DEBUG {
			errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s",
				r.Method,
				r.URL,
				err.Error())
		}
		fmt.Println(errorMessage)
		http.Error(w, errorMessage, 500)
		return true
	}
	return false
}

