package animation

// Code generated by cdproto-gen. DO NOT EDIT.

import (
	"fmt"
	"strings"

	"github.com/chromedp/cdproto/cdp"
	"github.com/chromedp/cdproto/dom"
)

// Animation animation instance.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Animation#type-Animation
type Animation struct {
	ID                   string                `json:"id"`                                      // Animation's id.
	Name                 string                `json:"name"`                                    // Animation's name.
	PausedState          bool                  `json:"pausedState"`                             // Animation's internal paused state.
	PlayState            string                `json:"playState"`                               // Animation's play state.
	PlaybackRate         float64               `json:"playbackRate"`                            // Animation's playback rate.
	StartTime            float64               `json:"startTime"`                               // Animation's start time. Milliseconds for time based animations and percentage [0 - 100] for scroll driven animations (i.e. when viewOrScrollTimeline exists).
	CurrentTime          float64               `json:"currentTime"`                             // Animation's current time.
	Type                 Type                  `json:"type"`                                    // Animation type of Animation.
	Source               *Effect               `json:"source,omitempty,omitzero"`               // Animation's source animation node.
	CSSID                string                `json:"cssId,omitempty,omitzero"`                // A unique ID for Animation representing the sources that triggered this CSS animation/transition.
	ViewOrScrollTimeline *ViewOrScrollTimeline `json:"viewOrScrollTimeline,omitempty,omitzero"` // View or scroll timeline
}

// ViewOrScrollTimeline timeline instance.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Animation#type-ViewOrScrollTimeline
type ViewOrScrollTimeline struct {
	SourceNodeID  cdp.BackendNodeID     `json:"sourceNodeId,omitempty,omitzero"`  // Scroll container node
	StartOffset   float64               `json:"startOffset,omitempty,omitzero"`   // Represents the starting scroll position of the timeline as a length offset in pixels from scroll origin.
	EndOffset     float64               `json:"endOffset,omitempty,omitzero"`     // Represents the ending scroll position of the timeline as a length offset in pixels from scroll origin.
	SubjectNodeID cdp.BackendNodeID     `json:"subjectNodeId,omitempty,omitzero"` // The element whose principal box's visibility in the scrollport defined the progress of the timeline. Does not exist for animations with ScrollTimeline
	Axis          dom.ScrollOrientation `json:"axis"`                             // Orientation of the scroll
}

// Effect animationEffect instance.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Animation#type-AnimationEffect
type Effect struct {
	Delay          float64           `json:"delay"`                            // AnimationEffect's delay.
	EndDelay       float64           `json:"endDelay"`                         // AnimationEffect's end delay.
	IterationStart float64           `json:"iterationStart"`                   // AnimationEffect's iteration start.
	Iterations     float64           `json:"iterations,omitempty,omitzero"`    // AnimationEffect's iterations. Omitted if the value is infinite.
	Duration       float64           `json:"duration"`                         // AnimationEffect's iteration duration. Milliseconds for time based animations and percentage [0 - 100] for scroll driven animations (i.e. when viewOrScrollTimeline exists).
	Direction      string            `json:"direction"`                        // AnimationEffect's playback direction.
	Fill           string            `json:"fill"`                             // AnimationEffect's fill mode.
	BackendNodeID  cdp.BackendNodeID `json:"backendNodeId,omitempty,omitzero"` // AnimationEffect's target node.
	KeyframesRule  *KeyframesRule    `json:"keyframesRule,omitempty,omitzero"` // AnimationEffect's keyframes.
	Easing         string            `json:"easing"`                           // AnimationEffect's timing function.
}

// KeyframesRule keyframes Rule.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Animation#type-KeyframesRule
type KeyframesRule struct {
	Name      string           `json:"name,omitempty,omitzero"` // CSS keyframed animation's name.
	Keyframes []*KeyframeStyle `json:"keyframes"`               // List of animation keyframes.
}

// KeyframeStyle keyframe Style.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Animation#type-KeyframeStyle
type KeyframeStyle struct {
	Offset string `json:"offset"` // Keyframe's time offset.
	Easing string `json:"easing"` // AnimationEffect's timing function.
}

// Type animation type of Animation.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Animation#type-Animation
type Type string

// String returns the Type as string value.
func (t Type) String() string {
	return string(t)
}

// Type values.
const (
	TypeCSSTransition Type = "CSSTransition"
	TypeCSSAnimation  Type = "CSSAnimation"
	TypeWebAnimation  Type = "WebAnimation"
)

// UnmarshalJSON satisfies [json.Unmarshaler].
func (t *Type) UnmarshalJSON(buf []byte) error {
	s := string(buf)
	s = strings.TrimSuffix(strings.TrimPrefix(s, `"`), `"`)

	switch Type(s) {
	case TypeCSSTransition:
		*t = TypeCSSTransition
	case TypeCSSAnimation:
		*t = TypeCSSAnimation
	case TypeWebAnimation:
		*t = TypeWebAnimation
	default:
		return fmt.Errorf("unknown Type value: %v", s)
	}
	return nil
}
