package models

import (
	"fmt"
	"github.com/google/uuid"
	"net/mail"
	"strings"
)

type SetNKodeResp struct {
	UserInterface []int `json:"user_interface"`
}

type RandomSvgInterfaceResp struct {
	Svgs   []string   `json:"svgs"`
	Colors []RGBColor `json:"colors"`
}

type RefreshTokenResp struct {
	AccessToken string `json:"access_token"`
}

type NewCustomerPost struct {
	NKodePolicy NKodePolicy `json:"nkode_policy"`
}

type GenerateSignupRestInterfacePost struct {
	CustomerId  string `json:"customer_id"`
	AttrsPerKey int    `json:"attrs_per_key"`
	NumbOfKeys  int    `json:"numb_of_keys"`
	UserEmail   string `json:"email"`
	Reset       bool   `json:"reset"`
}

type SetNKodePost struct {
	CustomerId   string `json:"customer_id"`
	KeySelection []int  `json:"key_selection"`
	SessionId    string `json:"session_id"`
}

type ConfirmNKodePost struct {
	CustomerId   string       `json:"customer_id"`
	KeySelection KeySelection `json:"key_selection"`
	SessionId    string       `json:"session_id"`
}

type GetLoginInterfacePost struct {
	UserEmail  string `json:"email"`
	CustomerId string `json:"customer_id"`
}

type LoginPost struct {
	CustomerId   string       `json:"customer_id"`
	UserEmail    string       `json:"email"`
	KeySelection KeySelection `json:"key_selection"`
}

type RenewAttributesPost struct {
	CustomerId string `json:"customer_id"`
}

type RefreshTokenPost struct {
	UserEmail  string `json:"email"`
	CustomerId string `json:"customer_id"`
}

type ResetNKodePost struct {
	UserEmail  string `json:"email"`
	CustomerId string `json:"customer_id"`
}

type CreateNewCustomerResp struct {
	CustomerId string `json:"customer_id"`
}

type GenerateSignupResetInterfaceResp struct {
	SessionId        string       `json:"session_id"`
	UserIdxInterface IdxInterface `json:"user_interface"`
	SvgInterface     []string     `json:"svg_interface"`
	Colors           []RGBColor   `json:"colors"`
}

type GetLoginInterfaceResp struct {
	UserIdxInterface IdxInterface `json:"user_interface"`
	SvgInterface     []string     `json:"svg_interface"`
	AttrsPerKey      int          `json:"attrs_per_key"`
	NumbOfKeys       int          `json:"numb_of_keys"`
	Colors           []RGBColor   `json:"colors"`
}

type KeySelection []int

type CustomerID uuid.UUID

func CustomerIdToString(customerId CustomerID) string {
	customerUuid := uuid.UUID(customerId)
	return customerUuid.String()
}

func CustomerIDFromString(customerID string) (CustomerID, error) {
	id, err := uuid.Parse(customerID)
	if err != nil {
		return CustomerID{}, err
	}
	return CustomerID(id), nil
}

type SessionId uuid.UUID
type UserId uuid.UUID

func UserIdFromString(userId string) UserId {
	id, err := uuid.Parse(userId)
	if err != nil {
		fmt.Errorf("unable to parse user id %+v", err)
	}
	return UserId(id)
}

func (s *SessionId) String() string {
	id := uuid.UUID(*s)
	return id.String()
}

type UserEmail string

func ParseEmail(email string) (UserEmail, error) {
	_, err := mail.ParseAddress(email)
	if err != nil {
		return "", err
	}
	return UserEmail(strings.ToLower(email)), err

}

type IdxInterface []int
type SvgIdInterface []int

func SessionIdFromString(sessionId string) (SessionId, error) {
	id, err := uuid.Parse(sessionId)
	if err != nil {
		return SessionId{}, err
	}

	return SessionId(id), nil
}

type EncipheredNKode struct {
	Code string
	Mask string
}

type RGBColor struct {
	Red   int `json:"red"`
	Green int `json:"green"`
	Blue  int `json:"blue"`
}

var SetColors = []RGBColor{
	{0, 0, 0},       // Black
	{255, 0, 0},     // Red
	{0, 128, 0},     // Dark Green
	{0, 0, 255},     // Blue
	{244, 200, 60},  // Yellow
	{255, 0, 255},   // Magenta
	{0, 200, 200},   // Cyan
	{127, 0, 127},   // Purple
	{232, 92, 13},   // Orange
	{0, 127, 127},   // Teal
	{127, 127, 0},   // Olive
	{127, 0, 0},     // Dark Red
	{128, 128, 128}, // Gray
	{228, 102, 102}, // Dark Purple
	{185, 17, 240},  // Salmon
	{16, 200, 100},  // Green
}
