package peers

import (
	"fmt"
	"io"
	"strconv"

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

	strfmt "github.com/go-openapi/strfmt"

	models "github.com/atotto/skyway-webrtc-gateway-go-client/models"
)

type PeerEventReader struct {
	formats strfmt.Registry
}

func (o *PeerEventReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
	switch response.Code() {

	case 200:
		result := NewPeerEventOK()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return result, nil

	case 400:
		result := NewPeerEventBadRequest()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 403:
		result := NewPeerEventForbidden()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 404:
		result := NewPeerEventNotFound()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 405:
		result := NewPeerEventMethodNotAllowed()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 406:
		result := NewPeerEventNotAcceptable()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 408:
		result := NewPeerEventRequestTimeout()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	default:
		return nil, runtime.NewAPIError("unknown error", response, response.Code())
	}
}

func NewPeerEventOK() *PeerEventOK {
	return &PeerEventOK{}
}

type PeerEventOK struct {
	Payload *models.PeerEventMessage
}

func (o *PeerEventOK) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventOK  %+v", 200, o.Payload)
}

func (o *PeerEventOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(models.PeerEventMessage)

	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

func NewPeerEventBadRequest() *PeerEventBadRequest {
	return &PeerEventBadRequest{}
}

type PeerEventBadRequest struct {
	Payload *PeerEventBadRequestBody
}

func (o *PeerEventBadRequest) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventBadRequest  %+v", 400, o.Payload)
}

func (o *PeerEventBadRequest) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(PeerEventBadRequestBody)

	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

func NewPeerEventForbidden() *PeerEventForbidden {
	return &PeerEventForbidden{}
}

type PeerEventForbidden struct {
}

func (o *PeerEventForbidden) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventForbidden ", 403)
}

func (o *PeerEventForbidden) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

func NewPeerEventNotFound() *PeerEventNotFound {
	return &PeerEventNotFound{}
}

type PeerEventNotFound struct {
}

func (o *PeerEventNotFound) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventNotFound ", 404)
}

func (o *PeerEventNotFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

func NewPeerEventMethodNotAllowed() *PeerEventMethodNotAllowed {
	return &PeerEventMethodNotAllowed{}
}

type PeerEventMethodNotAllowed struct {
}

func (o *PeerEventMethodNotAllowed) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventMethodNotAllowed ", 405)
}

func (o *PeerEventMethodNotAllowed) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

func NewPeerEventNotAcceptable() *PeerEventNotAcceptable {
	return &PeerEventNotAcceptable{}
}

type PeerEventNotAcceptable struct {
}

func (o *PeerEventNotAcceptable) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventNotAcceptable ", 406)
}

func (o *PeerEventNotAcceptable) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

func NewPeerEventRequestTimeout() *PeerEventRequestTimeout {
	return &PeerEventRequestTimeout{}
}

type PeerEventRequestTimeout struct {
}

func (o *PeerEventRequestTimeout) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventRequestTimeout ", 408)
}

func (o *PeerEventRequestTimeout) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

type PeerEventBadRequestBody struct {

	CommandType *string `json:"command_type"`

	Params *PeerEventBadRequestBodyParams `json:"params"`
}

func (o *PeerEventBadRequestBody) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

func (o *PeerEventBadRequestBody) validateCommandType(formats strfmt.Registry) error {

	if err := validate.Required("peerEventBadRequest"+"."+"command_type", "body", o.CommandType); err != nil {
		return err
	}

	return nil
}

func (o *PeerEventBadRequestBody) validateParams(formats strfmt.Registry) error {

	if err := validate.Required("peerEventBadRequest"+"."+"params", "body", o.Params); err != nil {
		return err
	}

	if o.Params != nil {
		if err := o.Params.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("peerEventBadRequest" + "." + "params")
			}
			return err
		}
	}

	return nil
}

func (o *PeerEventBadRequestBody) MarshalBinary() ([]byte, error) {
	if o == nil {
		return nil, nil
	}
	return swag.WriteJSON(o)
}

func (o *PeerEventBadRequestBody) UnmarshalBinary(b []byte) error {
	var res PeerEventBadRequestBody
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*o = res
	return nil
}

type PeerEventBadRequestBodyParams struct {

	Errors []*PeerEventBadRequestBodyParamsErrorsItems0 `json:"errors"`
}

func (o *PeerEventBadRequestBodyParams) Validate(formats strfmt.Registry) error {
	var res []error

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

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

func (o *PeerEventBadRequestBodyParams) validateErrors(formats strfmt.Registry) error {

	if err := validate.Required("peerEventBadRequest"+"."+"params"+"."+"errors", "body", o.Errors); err != nil {
		return err
	}

	for i := 0; i < len(o.Errors); i++ {
		if swag.IsZero(o.Errors[i]) { 
			continue
		}

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

	}

	return nil
}

func (o *PeerEventBadRequestBodyParams) MarshalBinary() ([]byte, error) {
	if o == nil {
		return nil, nil
	}
	return swag.WriteJSON(o)
}

func (o *PeerEventBadRequestBodyParams) UnmarshalBinary(b []byte) error {
	var res PeerEventBadRequestBodyParams
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*o = res
	return nil
}

type PeerEventBadRequestBodyParamsErrorsItems0 struct {

	Field string `json:"field,omitempty"`

	Message string `json:"message,omitempty"`
}

func (o *PeerEventBadRequestBodyParamsErrorsItems0) Validate(formats strfmt.Registry) error {
	return nil
}

func (o *PeerEventBadRequestBodyParamsErrorsItems0) MarshalBinary() ([]byte, error) {
	if o == nil {
		return nil, nil
	}
	return swag.WriteJSON(o)
}

func (o *PeerEventBadRequestBodyParamsErrorsItems0) UnmarshalBinary(b []byte) error {
	var res PeerEventBadRequestBodyParamsErrorsItems0
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*o = res
	return nil
}
// Code generated by go-swagger; DO NOT EDIT.

package peers

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

import (
	"fmt"
	"io"
	"strconv"

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

	strfmt "github.com/go-openapi/strfmt"

	models "github.com/atotto/skyway-webrtc-gateway-go-client/models"
)

// PeerEventReader is a Reader for the PeerEvent structure.
type PeerEventReader struct {
	formats strfmt.Registry
}

// ReadResponse reads a server response into the received o.
func (o *PeerEventReader) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
	switch response.Code() {

	case 200:
		result := NewPeerEventOK()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return result, nil

	case 400:
		result := NewPeerEventBadRequest()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 403:
		result := NewPeerEventForbidden()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 404:
		result := NewPeerEventNotFound()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 405:
		result := NewPeerEventMethodNotAllowed()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 406:
		result := NewPeerEventNotAcceptable()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	case 408:
		result := NewPeerEventRequestTimeout()
		if err := result.readResponse(response, consumer, o.formats); err != nil {
			return nil, err
		}
		return nil, result

	default:
		return nil, runtime.NewAPIError("unknown error", response, response.Code())
	}
}

// NewPeerEventOK creates a PeerEventOK with default headers values
func NewPeerEventOK() *PeerEventOK {
	return &PeerEventOK{}
}

/*PeerEventOK handles this case with default header values.

successful operation
*/
type PeerEventOK struct {
	Payload *models.PeerEventMessage
}

func (o *PeerEventOK) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventOK  %+v", 200, o.Payload)
}

func (o *PeerEventOK) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(models.PeerEventMessage)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

// NewPeerEventBadRequest creates a PeerEventBadRequest with default headers values
func NewPeerEventBadRequest() *PeerEventBadRequest {
	return &PeerEventBadRequest{}
}

/*PeerEventBadRequest handles this case with default header values.

Invalid input
*/
type PeerEventBadRequest struct {
	Payload *PeerEventBadRequestBody
}

func (o *PeerEventBadRequest) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventBadRequest  %+v", 400, o.Payload)
}

func (o *PeerEventBadRequest) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	o.Payload = new(PeerEventBadRequestBody)

	// response payload
	if err := consumer.Consume(response.Body(), o.Payload); err != nil && err != io.EOF {
		return err
	}

	return nil
}

// NewPeerEventForbidden creates a PeerEventForbidden with default headers values
func NewPeerEventForbidden() *PeerEventForbidden {
	return &PeerEventForbidden{}
}

/*PeerEventForbidden handles this case with default header values.

Forbidden(tokenが間違っている場合)
*/
type PeerEventForbidden struct {
}

func (o *PeerEventForbidden) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventForbidden ", 403)
}

func (o *PeerEventForbidden) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

// NewPeerEventNotFound creates a PeerEventNotFound with default headers values
func NewPeerEventNotFound() *PeerEventNotFound {
	return &PeerEventNotFound{}
}

/*PeerEventNotFound handles this case with default header values.

Not Found(peer_idが間違っている場合)
*/
type PeerEventNotFound struct {
}

func (o *PeerEventNotFound) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventNotFound ", 404)
}

func (o *PeerEventNotFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

// NewPeerEventMethodNotAllowed creates a PeerEventMethodNotAllowed with default headers values
func NewPeerEventMethodNotAllowed() *PeerEventMethodNotAllowed {
	return &PeerEventMethodNotAllowed{}
}

/*PeerEventMethodNotAllowed handles this case with default header values.

Method Not Allowed
*/
type PeerEventMethodNotAllowed struct {
}

func (o *PeerEventMethodNotAllowed) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventMethodNotAllowed ", 405)
}

func (o *PeerEventMethodNotAllowed) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

// NewPeerEventNotAcceptable creates a PeerEventNotAcceptable with default headers values
func NewPeerEventNotAcceptable() *PeerEventNotAcceptable {
	return &PeerEventNotAcceptable{}
}

/*PeerEventNotAcceptable handles this case with default header values.

Not Acceptable
*/
type PeerEventNotAcceptable struct {
}

func (o *PeerEventNotAcceptable) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventNotAcceptable ", 406)
}

func (o *PeerEventNotAcceptable) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

// NewPeerEventRequestTimeout creates a PeerEventRequestTimeout with default headers values
func NewPeerEventRequestTimeout() *PeerEventRequestTimeout {
	return &PeerEventRequestTimeout{}
}

/*PeerEventRequestTimeout handles this case with default header values.

Request Timeout
*/
type PeerEventRequestTimeout struct {
}

func (o *PeerEventRequestTimeout) Error() string {
	return fmt.Sprintf("[GET /peers/{peer_id}/events][%d] peerEventRequestTimeout ", 408)
}

func (o *PeerEventRequestTimeout) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error {

	return nil
}

/*PeerEventBadRequestBody peer event bad request body
swagger:model PeerEventBadRequestBody
*/
type PeerEventBadRequestBody struct {

	// command type
	// Required: true
	CommandType *string `json:"command_type"`

	// params
	// Required: true
	Params *PeerEventBadRequestBodyParams `json:"params"`
}

// Validate validates this peer event bad request body
func (o *PeerEventBadRequestBody) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

func (o *PeerEventBadRequestBody) validateCommandType(formats strfmt.Registry) error {

	if err := validate.Required("peerEventBadRequest"+"."+"command_type", "body", o.CommandType); err != nil {
		return err
	}

	return nil
}

func (o *PeerEventBadRequestBody) validateParams(formats strfmt.Registry) error {

	if err := validate.Required("peerEventBadRequest"+"."+"params", "body", o.Params); err != nil {
		return err
	}

	if o.Params != nil {
		if err := o.Params.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("peerEventBadRequest" + "." + "params")
			}
			return err
		}
	}

	return nil
}

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

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

/*PeerEventBadRequestBodyParams peer event bad request body params
swagger:model PeerEventBadRequestBodyParams
*/
type PeerEventBadRequestBodyParams struct {

	// errors
	// Required: true
	Errors []*PeerEventBadRequestBodyParamsErrorsItems0 `json:"errors"`
}

// Validate validates this peer event bad request body params
func (o *PeerEventBadRequestBodyParams) Validate(formats strfmt.Registry) error {
	var res []error

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

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

func (o *PeerEventBadRequestBodyParams) validateErrors(formats strfmt.Registry) error {

	if err := validate.Required("peerEventBadRequest"+"."+"params"+"."+"errors", "body", o.Errors); err != nil {
		return err
	}

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

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

	}

	return nil
}

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

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

/*PeerEventBadRequestBodyParamsErrorsItems0 peer event bad request body params errors items0
swagger:model PeerEventBadRequestBodyParamsErrorsItems0
*/
type PeerEventBadRequestBodyParamsErrorsItems0 struct {

	// field
	Field string `json:"field,omitempty"`

	// message
	Message string `json:"message,omitempty"`
}

// Validate validates this peer event bad request body params errors items0
func (o *PeerEventBadRequestBodyParamsErrorsItems0) Validate(formats strfmt.Registry) error {
	return nil
}

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

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