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

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

// OpsNote ops note
//
// swagger:model OpsNote
type OpsNote struct {

	// The user that created the Ops Note
	// Read Only: true
	CreatedBy string `json:"createdBy,omitempty"`

	// The date and time associated with the note, in epoch seconds format
	// Example: 1488826440
	HappenOnInSec int64 `json:"happenOnInSec,omitempty"`

	// The id associated with the Ops Note
	// Read Only: true
	ID string `json:"id,omitempty"`

	// The note message
	// Example: software update from 1.0.0 to 1.2.4
	// Required: true
	Note *string `json:"note"`

	scopesField []OpsNoteScope

	// The tags that should be associated with the note. Each tag has a unique id and a name - you can either include the name of a new or existing tag, or the id of an existing tag
	Tags []*OpsNoteTagBase `json:"tags,omitempty"`
}

// Scopes gets the scopes of this base type
func (m *OpsNote) Scopes() []OpsNoteScope {
	return m.scopesField
}

// SetScopes sets the scopes of this base type
func (m *OpsNote) SetScopes(val []OpsNoteScope) {
	m.scopesField = val
}

// UnmarshalJSON unmarshals this object with a polymorphic type from a JSON structure
func (m *OpsNote) UnmarshalJSON(raw []byte) error {
	var data struct {
		CreatedBy string `json:"createdBy,omitempty"`

		HappenOnInSec int64 `json:"happenOnInSec,omitempty"`

		ID string `json:"id,omitempty"`

		Note *string `json:"note"`

		Scopes json.RawMessage `json:"scopes,omitempty"`

		Tags []*OpsNoteTagBase `json:"tags,omitempty"`
	}
	buf := bytes.NewBuffer(raw)
	dec := json.NewDecoder(buf)
	dec.UseNumber()

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

	var propScopes []OpsNoteScope
	if string(data.Scopes) != "null" {
		scopes, err := UnmarshalOpsNoteScopeSlice(bytes.NewBuffer(data.Scopes), runtime.JSONConsumer())
		if err != nil && err != io.EOF {
			return err
		}
		propScopes = scopes
	}

	var result OpsNote

	// createdBy
	result.CreatedBy = data.CreatedBy

	// happenOnInSec
	result.HappenOnInSec = data.HappenOnInSec

	// id
	result.ID = data.ID

	// note
	result.Note = data.Note

	// scopes
	result.scopesField = propScopes

	// tags
	result.Tags = data.Tags

	*m = result

	return nil
}

// MarshalJSON marshals this object with a polymorphic type to a JSON structure
func (m OpsNote) MarshalJSON() ([]byte, error) {
	var b1, b2, b3 []byte
	var err error
	b1, err = json.Marshal(struct {
		CreatedBy string `json:"createdBy,omitempty"`

		HappenOnInSec int64 `json:"happenOnInSec,omitempty"`

		ID string `json:"id,omitempty"`

		Note *string `json:"note"`

		Tags []*OpsNoteTagBase `json:"tags,omitempty"`
	}{

		CreatedBy: m.CreatedBy,

		HappenOnInSec: m.HappenOnInSec,

		ID: m.ID,

		Note: m.Note,

		Tags: m.Tags,
	})
	if err != nil {
		return nil, err
	}
	b2, err = json.Marshal(struct {
		Scopes []OpsNoteScope `json:"scopes,omitempty"`
	}{

		Scopes: m.scopesField,
	})
	if err != nil {
		return nil, err
	}

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

// Validate validates this ops note
func (m *OpsNote) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

func (m *OpsNote) validateNote(formats strfmt.Registry) error {

	if err := validate.Required("note", "body", m.Note); err != nil {
		return err
	}

	return nil
}

func (m *OpsNote) validateScopes(formats strfmt.Registry) error {
	if swag.IsZero(m.Scopes()) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *OpsNote) validateTags(formats strfmt.Registry) error {
	if swag.IsZero(m.Tags) { // not required
		return nil
	}

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

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

	}

	return nil
}

// ContextValidate validate this ops note based on the context it is used
func (m *OpsNote) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

func (m *OpsNote) contextValidateCreatedBy(ctx context.Context, formats strfmt.Registry) error {

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

	return nil
}

func (m *OpsNote) contextValidateID(ctx context.Context, formats strfmt.Registry) error {

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

	return nil
}

func (m *OpsNote) contextValidateScopes(ctx context.Context, formats strfmt.Registry) error {

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

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

	}

	return nil
}

func (m *OpsNote) contextValidateTags(ctx context.Context, formats strfmt.Registry) error {

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

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

	}

	return nil
}

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

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