// Code generated by go-swagger; DO NOT EDIT.

package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"bytes"
	"context"
	"encoding/json"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"
	"github.com/go-openapi/validate"
)

// WebsiteSLAWidgetData website SLA widget data
//
// swagger:model WebsiteSLAWidgetData
type WebsiteSLAWidgetData struct {
	titleField string

	// availability
	// Read Only: true
	Availability float64 `json:"availability,omitempty"`

	// color level
	// Read Only: true
	ColorLevel int32 `json:"colorLevel,omitempty"`
}

// Title gets the title of this subtype
func (m *WebsiteSLAWidgetData) Title() string {
	return m.titleField
}

// SetTitle sets the title of this subtype
func (m *WebsiteSLAWidgetData) SetTitle(val string) {
	m.titleField = val
}

// Type gets the type of this subtype
func (m *WebsiteSLAWidgetData) Type() string {
	return "websiteSLA"
}

// SetType sets the type of this subtype
func (m *WebsiteSLAWidgetData) SetType(val string) {
}

// UnmarshalJSON unmarshals this object with a polymorphic type from a JSON structure
func (m *WebsiteSLAWidgetData) UnmarshalJSON(raw []byte) error {
	var data struct {

		// availability
		// Read Only: true
		Availability float64 `json:"availability,omitempty"`

		// color level
		// Read Only: true
		ColorLevel int32 `json:"colorLevel,omitempty"`
	}
	buf := bytes.NewBuffer(raw)
	dec := json.NewDecoder(buf)
	dec.UseNumber()

	if err := dec.Decode(&data); err != nil {
		return err
	}

	var base struct {
		/* Just the base type fields. Used for unmashalling polymorphic types.*/

		Title string `json:"title,omitempty"`

		Type string `json:"type,omitempty"`
	}
	buf = bytes.NewBuffer(raw)
	dec = json.NewDecoder(buf)
	dec.UseNumber()

	if err := dec.Decode(&base); err != nil {
		return err
	}

	var result WebsiteSLAWidgetData

	result.titleField = base.Title

	if base.Type != result.Type() {
		/* Not the type we're looking for. */
		return errors.New(422, "invalid type value: %q", base.Type)
	}

	result.Availability = data.Availability
	result.ColorLevel = data.ColorLevel

	*m = result

	return nil
}

// MarshalJSON marshals this object with a polymorphic type to a JSON structure
func (m WebsiteSLAWidgetData) MarshalJSON() ([]byte, error) {
	var b1, b2, b3 []byte
	var err error
	b1, err = json.Marshal(struct {

		// availability
		// Read Only: true
		Availability float64 `json:"availability,omitempty"`

		// color level
		// Read Only: true
		ColorLevel int32 `json:"colorLevel,omitempty"`
	}{

		Availability: m.Availability,

		ColorLevel: m.ColorLevel,
	})
	if err != nil {
		return nil, err
	}
	b2, err = json.Marshal(struct {
		Title string `json:"title,omitempty"`

		Type string `json:"type,omitempty"`
	}{

		Title: m.Title(),

		Type: m.Type(),
	})
	if err != nil {
		return nil, err
	}

	return swag.ConcatJSON(b1, b2, b3), nil
}

// Validate validates this website SLA widget data
func (m *WebsiteSLAWidgetData) Validate(formats strfmt.Registry) error {
	var res []error

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

// ContextValidate validate this website SLA widget data based on the context it is used
func (m *WebsiteSLAWidgetData) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	if err := m.contextValidateAvailability(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateColorLevel(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *WebsiteSLAWidgetData) contextValidateType(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "type", "body", string(m.Type())); err != nil {
		return err
	}

	return nil
}

func (m *WebsiteSLAWidgetData) contextValidateAvailability(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "availability", "body", float64(m.Availability)); err != nil {
		return err
	}

	return nil
}

func (m *WebsiteSLAWidgetData) contextValidateColorLevel(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "colorLevel", "body", int32(m.ColorLevel)); err != nil {
		return err
	}

	return nil
}

// MarshalBinary interface implementation
func (m *WebsiteSLAWidgetData) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return swag.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *WebsiteSLAWidgetData) UnmarshalBinary(b []byte) error {
	var res WebsiteSLAWidgetData
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
