package app

import (
	"database/sql"
	"time"

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

// UnMarshalCounty decodes swagger to first class object
func UnMarshalCounty(s *geo_models.County) *County {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	theInstances := []*TaxInstance{}
	for _, itm := range s.TaxInstances {
		theInstances = append(theInstances, UnMarshalTaxInstance(itm))
	}
	return &County{
		ID:                  s.ID,
		AccountID:           s.AccountID,
		Amount:              s.Amount,
		AreaDescription:     s.AreaDescription,
		ContactID:           s.ContactID,
		CountryID:           s.CountryID,
		EnrollmentStatus:    s.EnrollmentStatus,
		FIPS:                s.FIPS,
		FIPSclass:           s.FIPSclass,
		FunctionalStatus:    s.FunctionalStatus,
		Geocode:             s.Geocode,
		GNIS:                s.GNIS,
		Interest:            s.Interest,
		LandArea:            s.LandArea,
		LegalName:           s.LegalName,
		Latitude:            s.Latitude,
		Longitude:           s.Longitude,
		Name:                s.Name,
		Penalty:             s.Penalty,
		Ref:                 s.Ref,
		ReportedAdjustments: s.ReportedAdjustments,
		ReportedDeductions:  s.ReportedDeductions,
		ReportedNetRevenue:  s.ReportedNetRevenue,
		ReportedRate:        s.ReportedRate,
		ReportedRevenue:     s.ReportedNetRevenue,
		RevenueBase:         s.RevenueBase,
		RevenueNet:          s.RevenueNet,
		RevenueNotTaxable:   s.RevenueNotTaxable,
		StateID:             s.StateID,
		Status:              s.Status,
		Subtotal:            s.Subtotal,
		TaxInstances:        theInstances,
		TemplateID:          s.TemplateID,
		TotalAmount:         s.TotalAmount,
		TotalArea:           s.TotalArea,
		UnitBase:            s.UnitBase,
		WaterArea:           s.WaterArea,
		CreatedByID:         s.CreatedByID,
		HasDistrictTaxes:    s.HasDistrictTaxes,
		LastModifiedByID:    s.LastModifiedByID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *County) MarshalToSwagger() *geo_models.County {
	theInstances := []*geo_models.TaxInstance{}
	for _, itm := range obj.TaxInstances {
		theInstances = append(theInstances, itm.MarshalToSwagger())
	}
	var salesTaxRate *geo_models.TaxRate
	if obj.SalesTaxRate != nil {
		salesTaxRate = &geo_models.TaxRate{
			CombinedRate: obj.SalesTaxRate.CombinedRate,
			County:       obj.SalesTaxRate.County,
			CountyID:     obj.SalesTaxRate.CountyID,
			CountyRate:   obj.SalesTaxRate.CountyRate,
			Date:         obj.SalesTaxRate.Date,
			Focus:        obj.SalesTaxRate.Focus,
			Geocode:      obj.SalesTaxRate.Geocode,
			Place:        obj.SalesTaxRate.Place,
			PlaceID:      obj.SalesTaxRate.PlaceID,
			PlaceRate:    obj.SalesTaxRate.PlaceRate,
			State:        obj.SalesTaxRate.State,
			StateID:      obj.SalesTaxRate.StateID,
			StateRate:    obj.SalesTaxRate.StateRate,
		}
	}
	return &geo_models.County{
		ID:                  obj.ID,
		AccountID:           obj.AccountID,
		Amount:              obj.Amount,
		AreaDescription:     obj.AreaDescription,
		ContactID:           obj.ContactID,
		CountryID:           obj.CountryID,
		EnrollmentStatus:    obj.EnrollmentStatus,
		FIPS:                obj.FIPS,
		FIPSclass:           obj.FIPSclass,
		FunctionalStatus:    obj.FunctionalStatus,
		Geocode:             obj.Geocode,
		GNIS:                obj.GNIS,
		HasDistrictTaxes:    obj.HasDistrictTaxes,
		Interest:            obj.Interest,
		LandArea:            obj.LandArea,
		LegalName:           obj.LegalName,
		Latitude:            obj.Latitude,
		Longitude:           obj.Longitude,
		Name:                obj.Name,
		Penalty:             obj.Penalty,
		Ref:                 obj.Ref,
		ReportedAdjustments: obj.ReportedAdjustments,
		ReportedDeductions:  obj.ReportedDeductions,
		ReportedNetRevenue:  obj.ReportedNetRevenue,
		ReportedRate:        obj.ReportedRate,
		ReportedRevenue:     obj.ReportedRevenue,
		RevenueBase:         obj.RevenueBase,
		RevenueNet:          obj.RevenueNet,
		RevenueNotTaxable:   obj.RevenueNotTaxable,
		SalesTaxRate:        salesTaxRate,
		StateID:             obj.StateID,
		Status:              obj.Status,
		Subtotal:            obj.Subtotal,
		TaxInstances:        theInstances,
		TemplateID:          obj.TemplateID,
		TotalAmount:         obj.TotalAmount,
		TotalArea:           obj.TotalArea,
		UnitBase:            obj.UnitBase,
		WaterArea:           obj.WaterArea,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		OwnerID:             obj.OwnerID,
	}
}
