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

package ops

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

import (
	"context"
	"strconv"

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

// EntityApproval entity approval
//
// swagger:model entity.Approval
type EntityApproval struct {

	// Applicant is the user email who created the approval request.
	Applicant string `json:"applicant,omitempty"`

	// CCTasks defines the users who will be notified when the approval request is **Approved**.
	CcTasks []*EntityApprovalCCTask `json:"cc_tasks"`

	// DetailLink is the link to the detail page for this request.
	//
	// It is used in the notification message.
	DetailLink string `json:"detail_link,omitempty"`

	// EndAt is the time when the approval request is ended.
	EndAt int64 `json:"end_at,omitempty"`

	// id
	ID int64 `json:"id,omitempty"`

	// Name is the name of the approval request used to categorize approvals.
	Name string `json:"name,omitempty"`

	// Nodes defines the approval workflow.
	Nodes []*EntityApprovalNode `json:"nodes"`

	// Parameters is the parameters showed in the detail page.
	Parameters map[string]string `json:"parameters,omitempty"`

	// StartAt is the time when the approval request is created.
	StartAt int64 `json:"start_at,omitempty"`

	// status
	Status *EntityApprovalStatus `json:"status,omitempty"`
}

// Validate validates this entity approval
func (m *EntityApproval) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

func (m *EntityApproval) validateCcTasks(formats strfmt.Registry) error {
	if swag.IsZero(m.CcTasks) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *EntityApproval) validateNodes(formats strfmt.Registry) error {
	if swag.IsZero(m.Nodes) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *EntityApproval) validateStatus(formats strfmt.Registry) error {
	if swag.IsZero(m.Status) { // not required
		return nil
	}

	if m.Status != nil {
		if err := m.Status.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("status")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("status")
			}
			return err
		}
	}

	return nil
}

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

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

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

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

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

func (m *EntityApproval) contextValidateCcTasks(ctx context.Context, formats strfmt.Registry) error {

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

		if m.CcTasks[i] != nil {

			if swag.IsZero(m.CcTasks[i]) { // not required
				return nil
			}

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

	}

	return nil
}

func (m *EntityApproval) contextValidateNodes(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Nodes[i] != nil {

			if swag.IsZero(m.Nodes[i]) { // not required
				return nil
			}

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

	}

	return nil
}

func (m *EntityApproval) contextValidateStatus(ctx context.Context, formats strfmt.Registry) error {

	if m.Status != nil {

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

		if err := m.Status.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("status")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("status")
			}
			return err
		}
	}

	return nil
}

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

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