package pages

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/PuerkitoBio/goquery"
	matrixbot "roese.dev/matrix-development-lauge/matrix-bot"
	"roese.dev/matrix-development-lauge/modules/config"
	"roese.dev/matrix-development-lauge/modules/database"
)

func MelsungenNews() {
	log.Println("mels")
	res, err := http.Get("https://www.melsungen.de/aktuelles")

	if err != nil {
		log.Println("Failed to get response from melsungen news page")
		return
	}

	defer res.Body.Close()

	if res.StatusCode != 200 {
		log.Fatal("status code")
	}

	log.Println("ok")

	doc, err := goquery.NewDocumentFromReader(res.Body)

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

	log.Println("Find")

	db := database.GetDatabase()

	defer db.Close()

	var pageTitle string
	var firstPostTitle string

	stmt, err := db.Prepare("SELECT page_title FROM pages WHERE page_id = ?")

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

	defer stmt.Close()

	err = stmt.QueryRow("melsungen").Scan(&pageTitle)

	finish := false

	doc.Find(".post-item").Each(func(i int, s *goquery.Selection) {
		if !finish {
			date := s.Find(".date_label").Text()
			postTitle := s.Find(".post-title").Text()
			postUrl := s.Find(".post-title a").AttrOr("href", "")
			postExcerpt := s.Find(".post-excerpt").Text()

			if firstPostTitle == "" {
				firstPostTitle = postTitle
			}

			log.Printf("Review %d: %s %s %s\n", i, date, postTitle, postUrl)

			if err == sql.ErrNoRows {
				_, err = db.Exec(fmt.Sprintf("INSERT INTO pages(page_id, page_title) values('melsungen', '%s')", firstPostTitle))

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

				finish = true
			}

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

			if pageTitle == postTitle {
				_, err = db.Exec(fmt.Sprintf("UPDATE pages SET page_title = '%s' WHERE page_id = 'melsungen'", firstPostTitle))

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

				finish = true
			} else {
				matrixbot.SendMessage(config.Cfg.Pages[1].RoomID, "Melsungen - Aktuelles", postTitle, postExcerpt, postUrl)
			}

			time.Sleep(2 * time.Second)
		}
	})
}
