//
//  tank
//
//  Import and Playlist Daemon for autoradio project
//
//
//  Copyright (C) 2017-2018 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of tank.
//
//  tank is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  tank is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with tank. If not, see <http://www.gnu.org/licenses/>.
//

package importer

import (
	"errors"
	"net/url"
)

const (
	DefaultBacklog = 100
)

var (
	ErrNotImplemented     = errors.New("not implemented")
	ErrNotFound           = errors.New("not found")
	ErrSourceNotSupported = errors.New("source uri format is not supported")
	ErrFileNotNew         = errors.New("importing already running or done")
	ErrTooManyJobs        = errors.New("too many pending jobs")
	ErrTimeout            = errors.New("timeout")
)

type SourceURL url.URL

func (s *SourceURL) String() string {
	return (*url.URL)(s).String()
}

func (s *SourceURL) MarshalText() (data []byte, err error) {
	data = []byte(s.String())
	return
}

func (s *SourceURL) UnmarshalText(data []byte) (err error) {
	_, err = (*url.URL)(s).Parse(string(data))
	return
}

type JobState uint32

const (
	JobNew JobState = iota
	JobInitializing
	JobPending
	JobRunning
	JobDestroying
)

func (s JobState) String() string {
	switch s {
	case JobNew:
		return "new"
	case JobInitializing:
		return "initializing"
	case JobPending:
		return "pending"
	case JobRunning:
		return "running"
	case JobDestroying:
		return "destroying"
	}
	return "unknown"
}

func (s *JobState) fromString(str string) error {
	switch str {
	case "new":
		*s = JobNew
	case "initializing":
		*s = JobInitializing
	case "pending":
		*s = JobPending
	case "running":
		*s = JobRunning
	case "destroying":
		*s = JobDestroying
	default:
		return errors.New("invalid job state: '" + str + "'")
	}
	return nil
}

func (s JobState) MarshalText() (data []byte, err error) {
	data = []byte(s.String())
	return
}

func (s *JobState) UnmarshalText(data []byte) (err error) {
	return s.fromString(string(data))
}