package model

import (
    "time"
    //"strings"
    "net/url"
)


// TODO docstring
type Article struct {
    Identifier  int
    SourceUrl   string
    PublishDate time.Time
    FetchDate   time.Time
    Title       string
    Content     string
    Author      string
}

// TODO docstring
type ArticleViewModel struct {
    Title       string
    Author      string
    PublishDate string
    SourceUrl   string
    ShortSource string
    Summary     string
}


// TODO docstring
func (a *Article) ViewModel() *ArticleViewModel {
    summary := a.Content
    if len(a.Content) > 300 {
        summary = summary[:300]
    }

    short_url := ""
	parsedURL, err := url.Parse(a.SourceUrl)
    if err == nil {
        short_url = parsedURL.Hostname()

        //hostParts := strings.Split(short_url, ".")
	    //if len(hostParts) >= 2 {
        //    short_url = strings.Join(hostParts[len(hostParts)-2:], ".")
	    //}
    }

    return &ArticleViewModel{
        Title: a.Title,
        Author: a.Author,
        PublishDate: a.PublishDate.Local().Format("02.01.2006"),
        SourceUrl: a.SourceUrl,
        ShortSource: short_url,
        Summary: summary,
    }
}
