package rss

import (
	"fmt"
	"net/http"
	"strings"
	"testing"

	"github.com/mmcdole/gofeed"
)

func initRSSItem() {
	initRSSFeed()
}

func TestRSSItemNewEncodeDecode(t *testing.T) {
	initRSSItem()

	s := mockRSS()
	defer s.Close()

	resp, err := http.Get(s.URL)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	gofeed, err := gofeed.NewParser().Parse(resp.Body)
	if err != nil {
		t.Fatal(err)
	}
	gofeed.Items[0].Content = ""
	gofeed.Items[0].Description = ""

	item, err := newItem(gofeed.Items[0], ".*", "")
	if err != nil {
		t.Fatal(err)
	}

	itemB, err := newItem(gofeed.Items[0], "Podcast", "")
	if err != nil {
		t.Fatal(err)
	}
	if itemB.Content == item.Content {
		t.Errorf("content filter did not apply")
	}

	b, err := item.Encode()
	if err != nil {
		t.Fatal(err)
	}
	if err := itemB.Decode(b); err != nil {
		t.Fatal(err)
	}
	if itemB.Content != item.Content {
		t.Errorf("decode found %v, want %v", itemB.Content, item.Content)
	}

	if err := item.save("key"); err != nil {
		t.Fatal(err)
	}

	itemC := &Item{
		Link: item.Link,
	}
	if err := itemC.Load(item.ID(), "key"); err != nil {
		t.Fatal(err)
	}
	if fmt.Sprintf("%v", item) != fmt.Sprintf("%v", itemC) {
		t.Fatalf("%v != %v", item, itemC)
	}

	if err := itemC.Load(item.ID()+"not", "key"); err == nil {
		t.Fatal(err)
	}
	if err := itemC.Load(item.ID(), "key"+"not"); err == nil {
		t.Fatal(err)
	}
}

func TestClearBlankLines(t *testing.T) {
	cases := map[string]struct {
		in       string
		outLines int
	}{
		"remove with and without whitespace": {
			in: `<html>
<head>
   <script>something</script>
   <style>body{hello:"blue";}</style>
</head>
<body>
<br>
<br>
   <br>  
<br/><br/>
<br/> <br/>
</body>
         </html>`,
			outLines: 1,
		},
		"sample content": {
			in: `<html>
Linux
Blog
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<img src="https://www.jeremymorgan.com/images/programming-blog.png" alt="Programming
Blog"/><h1><a href="https://www.jeremymorgan.com/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Jeremy
Morgan</a></h1><h2>Mostly
Coherent
Ramblings
of
a
Silicon
Forest
Software
Developer</h2><br/><br/><br/>
<ul><br/>
<li>RSS</li><br/>
<br/></ul><br/>
<br/><br/>
<br/>
<br/>
<br/>
<br/><br/>
<br/><ul><li><a href="https://www.jeremymorgan.com/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Home</a></li><li><a href="https://www.jeremymorgan.com/tutorials/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Tutorials</a></li><li><a href="https://www.jeremymorgan.com/blog/programming/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Programming</a></li><li><a href="https://www.jeremymorgan.com/blog/raspberry-pi/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Raspberry
Pi</a></li><li><a href="https://www.jeremymorgan.com/blog/linux/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Linux</a></li><li><a href="https://www.jeremymorgan.com/blog/dotnet/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">.Net</a></li><li><a href="https://github.com/JeremyMorgan" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">My
Projects</a></li><li><a href="https://www.jeremymorgan.com/contact/" rel="noopener noreferrer" target="_blank" referrerpolicy="no-referrer">Contact
Me</a></li></ul><br/><br/>
<br/>
<br/>
<br/><br/>
<br/>
<br/>
<br/>
<h1>Trying
Out
         </html>`,
			outLines: 6,
		},
		"remove with and without whitespace 2": {
			in: `Daniel
Tillett</a><br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/>

<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
         `,
			outLines: 1,
		},
	}

	for name, c := range cases {
		out := clearBlankLines(c.in)
		cnts := []int{
			strings.Count(out, "<br>"),
			strings.Count(out, "<br/>"),
		}
		if v := cnts[0] + cnts[1]; v != c.outLines {
			t.Errorf("%v: want %v lines, got %v from %q: %q", name, c.outLines, v, c.in, out)
		}
	}
}
