/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package listenbrainz

import (
	"strconv"
	"time"

	"golang.org/x/exp/constraints"
)

type GetListensResult struct {
	Payload GetListenPayload `json:"payload"`
}

type GetListenPayload struct {
	Count                 int      `json:"count"`
	UserName              string   `json:"user_id"`
	LatestListenTimestamp int64    `json:"latest_listen_ts"`
	Listens               []Listen `json:"listens"`
}

type Listen struct {
	InsertedAt    int64  `json:"inserted_at"`
	ListenedAt    int64  `json:"listened_at"`
	RecordingMsid string `json:"recording_msid"`
	UserName      string `json:"user_name"`
	TrackMetadata Track  `json:"track_metadata"`
}

type Track struct {
	TrackName      string         `json:"track_name,omitempty"`
	ArtistName     string         `json:"artist_name,omitempty"`
	ReleaseName    string         `json:"release_name,omitempty"`
	AdditionalInfo map[string]any `json:"additional_info,omitempty"`
	MbidMapping    *MbidMapping   `json:"mbid_mapping,omitempty"`
}

type MbidMapping struct {
	RecordingName string   `json:"recording_name,omitempty"`
	RecordingMbid string   `json:"recording_mbid,omitempty"`
	ReleaseMbid   string   `json:"release_mbid,omitempty"`
	ArtistMbids   []string `json:"artist_mbids,omitempty"`
	Artists       []Artist `json:"artists,omitempty"`
}

type Artist struct {
	ArtistCreditName string `json:"artist_credit_name,omitempty"`
	ArtistMbid       string `json:"artist_mbid,omitempty"`
	JoinPhrase       string `json:"join_phrase,omitempty"`
}

type GetFeedbackResult struct {
	Count      int        `json:"count"`
	TotalCount int        `json:"total_count"`
	Offset     int        `json:"offset"`
	Feedback   []Feedback `json:"feedback"`
}

type Feedback struct {
	Created       int64  `json:"created,omitempty"`
	RecordingMbid string `json:"recording_mbid,omitempty"`
	RecordingMsid string `json:"recording_msid,omitempty"`
	Score         int    `json:"score,omitempty"`
	TrackMetadata *Track `json:"track_metadata,omitempty"`
	UserName      string `json:"user_id,omitempty"`
}

type LookupResult struct {
	ArtistCreditName string   `json:"artist_credit_name"`
	ReleaseName      string   `json:"release_name"`
	RecordingName    string   `json:"recording_name"`
	RecordingMbid    string   `json:"recording_mbid"`
	ReleaseMbid      string   `json:"release_mbid"`
	ArtistMbids      []string `json:"artist_mbids"`
}

type StatusResult struct {
	Status string `json:"status"`
}

type ErrorResult struct {
	Code  int    `json:"code"`
	Error string `json:"error"`
}

func (t Track) Duration() time.Duration {
	info := t.AdditionalInfo
	millisecondsF, ok := tryGetFloat[float64](info, "duration_ms")
	if ok {
		return time.Duration(int64(millisecondsF * float64(time.Millisecond)))
	}

	millisecondsI, ok := tryGetInteger[int64](info, "duration_ms")
	if ok {
		return time.Duration(millisecondsI * int64(time.Millisecond))
	}

	secondsF, ok := tryGetFloat[float64](info, "duration")
	if ok {
		return time.Duration(int64(secondsF * float64(time.Second)))
	}

	secondsI, ok := tryGetInteger[int64](info, "duration")
	if ok {
		return time.Duration(secondsI * int64(time.Second))
	}

	return time.Duration(0)
}

func (t Track) TrackNumber() int {
	value, ok := tryGetInteger[int](t.AdditionalInfo, "tracknumber")
	if ok {
		return value
	}
	return 0
}

func (t Track) DiscNumber() int {
	value, ok := tryGetInteger[int](t.AdditionalInfo, "discnumber")
	if ok {
		return value
	}
	return 0
}

func (t Track) ISRC() string {
	return tryGetValueOrEmpty[string](t.AdditionalInfo, "isrc")
}

func (t Track) RecordingMbid() string {
	mbid := tryGetValueOrEmpty[string](t.AdditionalInfo, "recording_mbid")
	if mbid == "" && t.MbidMapping != nil {
		return t.MbidMapping.RecordingMbid
	} else {
		return mbid
	}
}

func (t Track) ReleaseMbid() string {
	mbid := tryGetValueOrEmpty[string](t.AdditionalInfo, "release_mbid")
	if mbid == "" && t.MbidMapping != nil {
		return t.MbidMapping.ReleaseMbid
	} else {
		return mbid
	}
}

func (t Track) ReleaseGroupMbid() string {
	return tryGetValueOrEmpty[string](t.AdditionalInfo, "release_group_mbid")
}

func tryGetValueOrEmpty[T any](dict map[string]any, key string) T {
	var result T
	value, ok := dict[key].(T)
	if ok {
		result = value
	}
	return result
}

func tryGetFloat[T constraints.Float](dict map[string]any, key string) (T, bool) {
	valueFloat64, ok := dict[key].(float64)
	if ok {
		return T(valueFloat64), ok
	}

	valueFloat32, ok := dict[key].(float32)
	if ok {
		return T(valueFloat32), ok
	}

	valueStr, ok := dict[key].(string)
	if ok {
		valueFloat64, err := strconv.ParseFloat(valueStr, 64)
		if err == nil {
			return T(valueFloat64), ok
		}
	}

	return 0, false
}

func tryGetInteger[T constraints.Integer](dict map[string]any, key string) (T, bool) {
	valueInt64, ok := dict[key].(int64)
	if ok {
		return T(valueInt64), ok
	}

	valueInt32, ok := dict[key].(int32)
	if ok {
		return T(valueInt32), ok
	}

	valueInt, ok := dict[key].(int)
	if ok {
		return T(valueInt), ok
	}

	valueFloat, ok := tryGetFloat[float64](dict, key)
	if ok {
		return T(valueFloat), ok
	}

	return 0, false
}