package app

import (
	"database/sql"
	"reflect"
	"time"

	"code.tnxs.net/taxnexus/lib/api/ops/ops_models"
	"github.com/google/uuid"
)

// ReMarshalPaymentMethod converts microservice Models object to swagger
func ReMarshalPaymentMethod(p interface{}) ops_models.PaymentMethod {
	// reflect input and set to crm_models type
	t := reflect.ValueOf(&p).Elem()
	s := ops_models.PaymentMethod{}
	r := reflect.ValueOf(&s).Elem()
	for i := 0; i < t.NumField(); i++ {
		r.Field(i).Set(t.Field(i))
	}
	return s
}

// UnMarshalPaymentMethod converts swagger to first class object
func UnMarshalPaymentMethod(s *ops_models.PaymentMethod) *PaymentMethod {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	expirationDate, e2 := time.Parse(dateTimeFormat, s.ExpirationDate)
	return &PaymentMethod{
		ID:               s.ID,
		AccountID:        s.AccountID,
		AchAccountType:   s.AchAccountType,
		AchBankAccount:   s.AchBankAccount,
		AchRouting:       s.AchRouting,
		Active:           s.Active,
		Autopay:          s.Autopay,
		BankName:         s.BankName,
		BillingContactID: s.BillingContactID,
		CCnumber:         s.CCnumber,
		CCtype:           s.CCtype,
		CompanyID:        s.CompanyID,
		ContractID:       s.ContractID,
		CreatedByID:      s.CreatedByID,
		Default:          s.Default,
		ExpirationMonth:  s.ExpirationMonth,
		ExpirationYear:   s.ExpirationYear,
		Gateway:          s.Gateway,
		GatewayKey:       s.GatewayKey,
		LastModifiedByID: s.LastModifiedByID,
		Nickname:         s.Nickname,
		RecordType:       s.RecordType,
		Ref:              s.Ref,
		TenantID:         s.TenantID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		ExpirationDate: sql.NullTime{
			Time:  expirationDate,
			Valid: e2 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *PaymentMethod) MarshalToSwagger() *ops_models.PaymentMethod {
	return &ops_models.PaymentMethod{
		ID:               obj.ID,
		AccountID:        obj.AccountID,
		AchAccountType:   obj.AchAccountType,
		AchBankAccount:   obj.AchBankAccount,
		AchRouting:       obj.AchRouting,
		Active:           obj.Active,
		Autopay:          obj.Autopay,
		BankName:         obj.BankName,
		BillingContactID: obj.BillingContactID,
		CCnumber:         obj.CCnumber,
		CCtype:           obj.CCtype,
		CompanyID:        obj.CompanyID,
		ContractID:       obj.ContractID,
		CreatedByID:      obj.CreatedByID,
		CreatedDate:      obj.CreatedDate.Time.Format(dateTimeFormat),
		Default:          obj.Default,
		ExpirationDate:   obj.ExpirationDate.Time.Format(dateTimeFormat),
		ExpirationMonth:  obj.ExpirationMonth,
		ExpirationYear:   obj.ExpirationYear,
		Gateway:          obj.Gateway,
		GatewayKey:       obj.GatewayKey,
		LastModifiedByID: obj.LastModifiedByID,
		LastModifiedDate: obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Nickname:         obj.Nickname,
		RecordType:       obj.RecordType,
		Ref:              obj.Ref,
	}
}
