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

// (c) 2012-2020 by Taxnexus, Inc.
// All rights reserved worldwide.
// Proprietary product; unlicensed use is not allowed

package research_models

import (
	"context"
	stderrors "errors"
	"strconv"

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

// Topic A research topic that collects data
//
// swagger:model topic
type Topic struct {

	// Created By User ID
	CreatedByID *string `json:"CreatedByID,omitempty"`

	// Created Date
	CreatedDate *string `json:"CreatedDate,omitempty"`

	// Topic Description
	Description *string `json:"Description,omitempty"`

	// The list of Factors used to analyze this industry
	Factors []*Factor `json:"Factors"`

	// Record Id
	ID string `json:"ID,omitempty"`

	// Last Modified By User ID
	LastModifiedByID *string `json:"LastModifiedByID,omitempty"`

	// Last Modified Date
	LastModifiedDate *string `json:"LastModifiedDate,omitempty"`

	// Topic Name
	Name *string `json:"Name,omitempty"`

	// The ID of the Parent Topic
	ParentTopicID *string `json:"ParentTopicID,omitempty"`

	// The CMS Slug for this Topic
	Slug *string `json:"Slug,omitempty"`
}

// Validate validates this topic
func (m *Topic) Validate(formats strfmt.Registry) error {
	var res []error

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

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

func (m *Topic) validateFactors(formats strfmt.Registry) error {
	if typeutils.IsZero(m.Factors) { // not required
		return nil
	}

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

		if m.Factors[i] != nil {
			if err := m.Factors[i].Validate(formats); err != nil {
				ve := new(errors.Validation)
				if stderrors.As(err, &ve) {
					return ve.ValidateName("Factors" + "." + strconv.Itoa(i))
				}
				ce := new(errors.CompositeError)
				if stderrors.As(err, &ce) {
					return ce.ValidateName("Factors" + "." + strconv.Itoa(i))
				}

				return err
			}
		}

	}

	return nil
}

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

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

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

func (m *Topic) contextValidateFactors(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Factors[i] != nil {

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

			if err := m.Factors[i].ContextValidate(ctx, formats); err != nil {
				ve := new(errors.Validation)
				if stderrors.As(err, &ve) {
					return ve.ValidateName("Factors" + "." + strconv.Itoa(i))
				}
				ce := new(errors.CompositeError)
				if stderrors.As(err, &ce) {
					return ce.ValidateName("Factors" + "." + strconv.Itoa(i))
				}

				return err
			}
		}

	}

	return nil
}

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

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