package app

import (
	"database/sql"
	"time"

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

// UnMarshalPlace decodes swagger to first class object
func UnMarshalPlace(s *geo_models.Place) *Place {
	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 &Place{
		ID:                  s.ID,
		AccountID:           s.AccountID,
		AccountValidation:   s.AccountValidation,
		Amount:              s.Amount,
		AreaDescription:     s.AreaDescription,
		ContactID:           s.ContactID,
		CountryID:           s.CountryID,
		CountyID:            s.CountyID,
		CreatedByID:         s.CreatedByID,
		EnrollmentStatus:    s.EnrollmentStatus,
		FIPS:                s.FIPS,
		FIPSclass:           s.FIPSclass,
		FunctionalStatus:    s.FunctionalStatus,
		Geocode:             s.Geocode,
		GNIS:                s.GNIS,
		HasDistrictTaxes:    s.HasDistrictTaxes,
		Interest:            s.Interest,
		Landarea:            s.LandArea,
		LastModifiedByID:    s.LastModifiedByID,
		Latitude:            s.Latitude,
		LegalName:           s.LegalName,
		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.ReportedRevenue,
		RevenueBase:         s.RevenueBase,
		RevenueNet:          s.RevenueNet,
		RevenueNotTaxable:   s.RevenueNotTaxable,
		Revneuebase:         s.RevenueBase,
		StateCode:           s.StateCode,
		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,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *Place) MarshalToSwagger() *geo_models.Place {
	if obj == nil {
		return nil
	}
	taxInstances := []*geo_models.TaxInstance{}
	for _, itm := range obj.TaxInstances {
		taxInstances = append(taxInstances, 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.Place{
		ID:                  obj.ID,
		AccountID:           obj.AccountID,
		AccountValidation:   obj.AccountValidation,
		Amount:              obj.Amount,
		AreaDescription:     obj.AreaDescription,
		ContactID:           obj.ContactID,
		CountryID:           obj.CountryID,
		CountyID:            obj.CountyID,
		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:        taxInstances,
		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,
		StateCode:           obj.StateCode,
	}
}
