package main

import (
	"container/list"
	"errors"
	"fmt"
	"io"
	"net/http"
	"os"
	"os/exec"
	"strings"

	"./crawler"
)

// ListPagesComics ...
func ListPagesComics(url string) *list.List {
	var list = list.New()
	var crawl = crawler.New(url)
	listUrls := crawl.ListLink()
	for elem := listUrls.Front(); elem != nil; elem = elem.Next() {
		image := elem.Value.(string)
		if strings.Contains(image, ".jpg") {
			file := fmt.Sprintf("%s%s", url, image)
			list.PushBack(file)
		}
	}
	return list
}

// DownloadFile ...
func DownloadFile(url string, filepath string) error {
	out, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Get the data
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	if resp.StatusCode == 404 || resp.ContentLength == -1 {
		return errors.New("File not found.")
	}

	defer resp.Body.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	if err != nil {
		return err
	}

	return nil
}

func Exists(name string) bool {
	if _, err := os.Stat(name); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

func main() {
	path := "./dr-stone"
	pathFile7z := fmt.Sprintf("%s%s", path, "/dr-stone-t")
	for i := 132; i <= 160; i++ {
		folder := fmt.Sprintf("%s/T%d", path, i)
		var page = 0
		notErr := true
		for notErr {
			url := fmt.Sprintf("http://lelscano.com//mangas/dr-stone/%d/", i)
			if page < 10 {
				url = fmt.Sprintf(url+"0%d.jpg", page)
			} else {
				url = fmt.Sprintf(url+"%d.jpg", page)
			}
			fmt.Println(url)
			if Exists(folder) == false {
				os.MkdirAll(folder, os.ModePerm)
			}
			file := fmt.Sprintf("%s/ep%d.jpg", folder, page)
			if Exists(file) == false {
				err := DownloadFile(url, file)
				if err != nil {
					fmt.Println(err)
					os.Remove(file)
					notErr = false
				} else {
					fmt.Println("", folder, " => ", page)
					page++
				}
			}
		}
		fileCompress := fmt.Sprintf("%s%d.cbz", pathFile7z, i)
		cmd := exec.Command("7z", "a", fileCompress, folder)
		err := cmd.Run()
		if err != nil {
			fmt.Println(err)
		}
	}
}
