// 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"
	"strconv"

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

// DeviceSLAWidgetData device SLA widget data
//
// swagger:model DeviceSLAWidgetData
type DeviceSLAWidgetData struct {
	titleField string

	// result list
	// Read Only: true
	ResultList []*ResultItem `json:"resultList,omitempty"`
}

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

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

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

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

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

		// result list
		// Read Only: true
		ResultList []*ResultItem `json:"resultList,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 DeviceSLAWidgetData

	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.ResultList = data.ResultList

	*m = result

	return nil
}

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

		// result list
		// Read Only: true
		ResultList []*ResultItem `json:"resultList,omitempty"`
	}{

		ResultList: m.ResultList,
	})
	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 device SLA widget data
func (m *DeviceSLAWidgetData) Validate(formats strfmt.Registry) error {
	var res []error

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

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

func (m *DeviceSLAWidgetData) validateResultList(formats strfmt.Registry) error {

	if swag.IsZero(m.ResultList) { // not required
		return nil
	}

	for i := 0; i < len(m.ResultList); i++ {
		if swag.IsZero(m.ResultList[i]) { // not required
			continue
		}

		if m.ResultList[i] != nil {
			if err := m.ResultList[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("resultList" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

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

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

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

func (m *DeviceSLAWidgetData) 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 *DeviceSLAWidgetData) contextValidateResultList(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "resultList", "body", []*ResultItem(m.ResultList)); err != nil {
		return err
	}

	for i := 0; i < len(m.ResultList); i++ {

		if m.ResultList[i] != nil {
			if err := m.ResultList[i].ContextValidate(ctx, formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("resultList" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

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

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