package media

import (
	"fmt"
	"image"
	"image/jpeg"
	"io"
	"os"
	"path"
	"path/filepath"
	"regexp"
	"sort"
	"strconv"
	"time"

	// for processing images
	_ "image/gif"
	_ "image/png"

	"github.com/nfnt/resize"
	"github.com/tajtiattila/metadata"
)

type MediaSource struct {
	StorageDir string
	ThumbDir   string

	set Set
}

type Media struct {
	Type     string
	Name     string
	Size     image.Rectangle
	FullName string

	metadata *metadata.Metadata
}

func (ms *MediaSource) LocationOrig(m Media) string {
	return path.Join(ms.StorageDir, m.Name)
}

func (ms *MediaSource) ThumbPath(m Media, size image.Rectangle) string {
	w := size.Dx()
	h := size.Dy()

	var ws, hs string

	if w != 0 {
		ws = fmt.Sprint(w)
	}
	if h != 0 {
		hs = fmt.Sprint(h)
	}

	thumbSlug := filepath.Join(fmt.Sprintf("%sx%s", ws, hs), m.Name)
	return path.Join("/media", thumbSlug)
}

func (ms *MediaSource) ThumbFilename(m Media, size image.Rectangle) string {
	return filepath.Join(ms.ThumbDir, ms.ThumbPath(m, size))
}

func (ms *MediaSource) ReceiveNewMedia(name string, r io.Reader) error {
	ms.set = nil

	dest := path.Join(ms.StorageDir, name)
	f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		return err
	}

	_, err = io.Copy(f, r)
	if err != nil {
		return err
	}

	return f.Close()
}

func (m *Media) Date() time.Time {
	m.getMetadata()

	if m.metadata != nil {
		raw := m.metadata.Get(metadata.DateTimeOriginal)
		if raw != "" {
			d, err := time.Parse("2006-01-02T15:04:05", raw)
			if err == nil {
				return d
			}
		}
	}

	fi, err := os.Stat(m.FullName)
	if err == nil {
		return fi.ModTime()
	}

	return time.Time{}
}

func (m *Media) getMetadata() error {
	if m.metadata != nil {
		return nil
	}

	f, err := os.Open(m.FullName)
	if err != nil {
		return err
	}
	md, err := metadata.Parse(f)
	if err != nil {
		return err
	}
	m.metadata = md
	return nil
}

func (ms *MediaSource) Size(name string) (image.Rectangle, error) {
	f, err := os.Open(name)
	if err != nil {
		return image.ZR, err
	}
	defer f.Close()

	cfg, _, err := image.DecodeConfig(f)
	if err != nil {
		return image.ZR, err
	}

	width := cfg.Width
	height := cfg.Height

	return image.Rect(0, 0, width, height), nil
}

func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, image.Rectangle, error) {
	f, err := os.Open(ms.LocationOrig(m))
	if err != nil {
		return "", image.ZR, err
	}
	defer f.Close()

	cfg, _, err := image.DecodeConfig(f)
	if err != nil {
		return "", image.ZR, err
	}

	width := cfg.Width
	height := cfg.Height

	if width > height {
		height = height * maxDim / width
		width = maxDim
	} else {
		width = width * maxDim / height
		height = maxDim
	}

	size := image.Rect(0, 0, width, height)
	if ms.HasThumb(m, size) {
		return ms.ThumbPath(m, size), size, nil
	}

	_, err = f.Seek(0, io.SeekStart)
	if err != nil {
		return "", image.ZR, err
	}

	src, err := ms.thumbReader(f, m, size)
	return src, size, err
}

func (ms *MediaSource) HasThumb(m Media, size image.Rectangle) bool {
	fi, err := os.Stat(ms.ThumbFilename(m, size))
	if err != nil {
		return false
	}
	return m.Date().Before(fi.ModTime())
}

func (ms *MediaSource) ByName(name string) *Media {
	size, _ := ms.Size(path.Join(ms.StorageDir, name))
	m := Media{
		Type: "image",
		Name: name,
		Size: size,
	}
	m.FullName = ms.LocationOrig(m)
	return &m
}

func (ms *MediaSource) Thumb(m Media, size image.Rectangle) (string, error) {
	if ms.HasThumb(m, size) {
		return ms.ThumbPath(m, size), nil
	}

	f, err := os.Open(ms.LocationOrig(m))
	if err != nil {
		return "", err
	}
	defer f.Close()

	return ms.thumbReader(f, m, size)
}

func (ms *MediaSource) thumbReader(r io.Reader, m Media, size image.Rectangle) (string, error) {
	img, _, err := image.Decode(r)
	if err != nil {
		return "", err
	}

	return ms.ThumbImage(img, m, size)
}

func (ms *MediaSource) ThumbImage(img image.Image, m Media, size image.Rectangle) (string, error) {
	thumbLoc := ms.ThumbFilename(m, size)
	os.MkdirAll(path.Dir(thumbLoc), 0755)
	fthumb, err := os.OpenFile(thumbLoc, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0655)
	if err != nil {
		return "", err
	}

	img = resize.Resize(uint(size.Dx()), uint(size.Dy()), img, resize.Bilinear)

	err = jpeg.Encode(fthumb, img, nil)
	if err != nil {
		return "", err
	}

	err = fthumb.Close()
	if err != nil {
		return "", err
	}

	return ms.ThumbPath(m, size), nil
}

func (ms *MediaSource) Walk() ([]*Media, error) {
	if ms.set == nil {
		var media []*Media

		err := filepath.Walk(ms.StorageDir, func(name string, fi os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			if fi.IsDir() {
				return nil
			}
			media = append(media, ms.ByName(path.Base(name)))
			return nil
		})

		if err != nil {
			return media, err
		}

		ms.set = media
	}

	return ms.set, nil
}

type Set []*Media

func (s Set) ByDate() Set {
	sort.Slice(s, func(i, j int) bool {
		return s[i].Date().After(s[j].Date())
	})
	return s
}

var (
	sizeString = regexp.MustCompile(`([0-9]*)(x)?([0-9]*)`)
)

func ParseSizeString(str string, actual image.Rectangle) (image.Rectangle, error) {
	var err = fmt.Errorf("expected a size string {width}x{height}, saw %q", str)

	strs := sizeString.FindStringSubmatch(str)
	if len(strs) < 4 {
		return image.ZR, err
	}

	var w, h int
	var strconvErr error

	if strs[1] != "" {
		w, strconvErr = strconv.Atoi(strs[1])
		if strconvErr != nil {
			return image.ZR, err
		}
	}

	if strs[3] != "" {
		h, strconvErr = strconv.Atoi(strs[3])
		if strconvErr != nil {
			return image.ZR, err
		}
	}

	if strs[2] != "x" {
		// w was the only dimension given, so set it to the greater dimension
		// of the actual image size
		if actual.Dx() > actual.Dy() {
			h = 0
		} else {
			h = w
			w = 0
		}
	}

	return image.Rect(0, 0, w, h), nil
}

func removeExtension(name string) string {
	ext := path.Ext(name)
	return name[:len(name)-len(ext)]
}
