package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
	"github.com/google/uuid"
)

// UnMarshalTaxType decodes swagger to a first class object
func UnMarshalTaxType(s *geo_models.TaxType) *TaxType {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	dateTime, dateErr := time.Parse(dateTimeFormat, s.CreatedDate)
	createdDate := sql.NullTime{
		Time:  dateTime,
		Valid: dateErr == nil,
	}
	dateTime, dateErr = time.Parse(dateTimeFormat, s.LastModifiedDate)
	modifiedDate := sql.NullTime{
		Time:  dateTime,
		Valid: dateErr == nil,
	}
	date, dateErr := time.Parse(dateFormat, s.EffectiveDate)
	effectiveDate := sql.NullTime{
		Time:  date,
		Valid: dateErr == nil,
	}
	date, dateErr = time.Parse(dateFormat, s.EndDate)
	endDate := sql.NullTime{
		Time:  date,
		Valid: dateErr == nil,
	}
	return &TaxType{
		ID:                 s.ID,
		AccountID:          s.AccountID,
		AccountingRuleCode: s.AccountingRuleCode,
		Active:             s.Active,
		AgencyType:         s.AgencyType,
		AgentID:            s.AgentID,
		Amount:             s.Amount,
		Category:           s.Category,
		CollectorDomainID:  s.CollectorDomainID,
		CompanyID:          s.CompanyID,
		ContactID:          s.ContactID,
		CreatedByID:        s.CreatedByID,
		CreatedDate:        createdDate,
		Description:        s.Description,
		EffectiveDate:      effectiveDate,
		EndDate:            endDate,
		EnrollmentStatus:   s.EnrollmentStatus,
		FilingCity:         s.FilingCity,
		FilingCountry:      s.FilingCountry,
		FilingEmail:        s.FilingEmail,
		FilingMethod:       s.FilingMethod,
		FilingPostalcode:   s.FilingPostalCode,
		FilingState:        s.FilingState,
		FilingStreet:       s.FilingStreet,
		Fractional:         s.Fractional,
		Frequency:          s.Frequency,
		GeocodeString:      s.GeocodeString,
		InterestRate:       s.InterestRate,
		IsMedicinal:        s.IsMedicinal,
		IsRecreational:     s.IsRecreational,
		LastModifiedByID:   s.LastModifiedByID,
		LastModifiedDate:   modifiedDate,
		MarkupRate:         s.MarkupRate,
		Name:               s.Name,
		OwnerID:            s.OwnerID,
		Passthrough:        s.PassThrough,
		PenaltyDays:        s.PenaltyDays,
		PenaltyRate:        s.PenaltyRate,
		Rate:               s.Rate,
		Reference:          s.Reference,
		RevenueBase:        s.RevenueBase,
		RevenueNet:         s.RevenueNet,
		RevenueNotTaxable:  s.RevenueNotTaxable,
		SalesRegulation:    s.SalesRegulation,
		Status:             s.Status,
		TaxnexusCodeID:     s.TaxnexusCodeID,
		TaxnexusNumber:     s.TaxnexusNumber,
		TemplateID:         s.TemplateID,
		UnitBase:           s.UnitBase,
		Units:              s.Units,
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *TaxType) MarshalToSwagger() *geo_models.TaxType {
	return &geo_models.TaxType{
		ID:                 obj.ID,
		AccountID:          obj.AccountID,
		AccountingRuleCode: obj.AccountingRuleCode,
		Active:             obj.Active,
		AgencyType:         obj.AgencyType,
		AgentID:            obj.AgentID,
		Amount:             obj.Amount,
		Category:           obj.Category,
		CollectorDomainID:  obj.CollectorDomainID,
		CompanyID:          obj.CompanyID,
		ContactID:          obj.ContactID,
		CreatedByID:        obj.CreatedByID,
		CreatedDate:        obj.CreatedDate.Time.Format(dateTimeFormatAlt),
		Description:        obj.Description,
		EffectiveDate:      obj.EffectiveDate.Time.Format(dateFormat),
		EndDate:            obj.EndDate.Time.Format(dateFormat),
		EnrollmentStatus:   obj.EnrollmentStatus,
		FilingCity:         obj.FilingCity,
		FilingCountry:      obj.FilingCountry,
		FilingEmail:        obj.FilingEmail,
		FilingMethod:       obj.FilingMethod,
		FilingPostalCode:   obj.FilingPostalcode,
		FilingState:        obj.FilingState,
		FilingStreet:       obj.FilingStreet,
		Fractional:         obj.Fractional,
		Frequency:          obj.Frequency,
		GeocodeString:      obj.GeocodeString,
		InterestRate:       obj.InterestRate,
		IsMedicinal:        obj.IsMedicinal,
		IsRecreational:     obj.IsRecreational,
		LastModifiedByID:   obj.LastModifiedByID,
		LastModifiedDate:   obj.LastModifiedDate.Time.Format(dateTimeFormatAlt),
		MarkupRate:         obj.MarkupRate,
		Name:               obj.Name,
		OwnerID:            obj.OwnerID,
		PassThrough:        obj.Passthrough,
		PenaltyDays:        obj.PenaltyDays,
		PenaltyRate:        obj.PenaltyRate,
		Rate:               obj.Rate,
		Reference:          obj.Reference,
		RevenueBase:        obj.RevenueBase,
		RevenueNet:         obj.RevenueNet,
		RevenueNotTaxable:  obj.RevenueNotTaxable,
		SalesRegulation:    obj.SalesRegulation,
		Status:             obj.Status,
		TaxnexusCodeID:     obj.TaxnexusCodeID,
		TaxnexusNumber:     obj.TaxnexusNumber,
		TemplateID:         obj.TemplateID,
		UnitBase:           obj.UnitBase,
		Units:              obj.Units,
	}
}
