package main

import (
	"C"
	"errors"
	"fmt"
	"net/http"
	"strings"
	"time"

	"github.com/PuerkitoBio/goquery"
)

func GetSourceName() string {
	return "gocomics"
}

func GetFeedList() []map[string]string {
	var ret []map[string]string
	lstUrl := "http://www.gocomics.com/comics/a-to-z"
	res, err := http.Get(lstUrl)
	if err != nil {
		fmt.Println("Error getting gocomics list:", err.Error())
		return ret
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		fmt.Println("Error getting gocomics list:", res.StatusCode)
		return ret
	}

	// Load the HTML document
	doc, err := goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		fmt.Println("Error loading gocomics list:", err.Error())
		return ret
	}
	doc.Find(".gc-blended-link").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the band and title
		slug, exists := s.Attr("href")
		if exists {
			pts := strings.Split(slug, "/")
			if len(pts) > 2 {
				slug = pts[1]
			}
			inner := strings.TrimSpace(s.Text())
			pts = strings.Split(inner, "\n")
			name := strings.TrimSpace(pts[0])
			var author string
			if len(pts) > 1 {
				author = strings.TrimSpace(pts[1])
			}
			author = strings.TrimPrefix(author, "By ")
			author = strings.Replace(author, "\u0026", "&", -1)
			feedMap := make(map[string]string)
			feedMap["slug"] = slug
			feedMap["name"] = name
			feedMap["author"] = author
			feedMap["source"] = GetSourceName()
			ret = append(ret, feedMap)
		}
	})
	return ret
}

func GetFeedUrl(slug string, date time.Time) (string, error) {
	return fmt.Sprintf(
		"http://www.gocomics.com/%s/%04d/%02d/%02d",
		slug,
		date.Year(),
		date.Month(),
		date.Day(),
	), nil
}

func GetFeedDesc(slug string, date time.Time) (string, error) {
	url, err := GetFeedUrl(slug, date)
	if err != nil {
		return "", err
	}
	res, err := http.Get(url)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		return "", errors.New(fmt.Sprintf("Status code error: %d %s", res.StatusCode, res.Status))
	}

	// Load the HTML document
	doc, err := goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		return "", err
	}
	// Find the Picture
	src, exists := doc.Find("picture.item-comic-image>img").Attr("src")
	if !exists {
		return "", errors.New("Couldn't find image source")
	}
	return "<img src=\"" + src + "\" />", nil
}
