package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
)

// UnMarshalSwaggerState decodes swagger to first class object
func UnMarshalSwaggerState(s *geo_models.State) *State {
	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 &State{
		ID:                  s.ID,
		AccountID:           s.AccountID,
		Amount:              s.Amount,
		Code:                s.Code,
		ContactID:           s.ContactID,
		CountryID:           s.CountryID,
		CreatedByID:         s.CreatedByID,
		Division:            s.Division,
		EnrollmentStatus:    s.EnrollmentStatus,
		FIPS:                s.FIPS,
		Geocode:             s.Geocode,
		GNIS:                s.GNIS,
		Interest:            s.Interest,
		LandArea:            s.LandArea,
		LastModifiedByID:    s.LastModifiedByID,
		Latitude:            s.Latitude,
		Longitude:           s.Longitude,
		Name:                s.Name,
		OwnerID:             s.OwnerID,
		Penalty:             s.Penalty,
		Ref:                 s.Ref,
		Region:              s.Region,
		ReportedAdjustments: s.ReportedAdjustments,
		ReportedDeductions:  s.ReportedDeductions,
		ReportedNetRevenue:  s.ReportedNetRevenue,
		ReportedRate:        s.ReportedRate,
		ReportedRevenue:     s.ReportedRevenue,
		RevenueBase:         s.RevenueBase,
		RevenueNet:          s.RevenueNet,
		RevenueNotTaxable:   s.RevenueNotTaxable,
		SGC:                 s.SGC,
		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 *State) MarshalToSwagger() *geo_models.State {
	taxInstances := []*geo_models.TaxInstance{}
	for _, itm := range obj.TaxInstances {
		taxInstances = append(taxInstances, itm.MarshalToSwagger())
	}
	return &geo_models.State{
		ID:                  obj.ID,
		AccountID:           obj.AccountID,
		Amount:              obj.Amount,
		Code:                obj.Code,
		ContactID:           obj.ContactID,
		CountryID:           obj.CountryID,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		Division:            obj.Division,
		EnrollmentStatus:    obj.EnrollmentStatus,
		FIPS:                obj.FIPS,
		Geocode:             obj.Geocode,
		GNIS:                obj.GNIS,
		Interest:            obj.Interest,
		LandArea:            obj.LandArea,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Latitude:            obj.Latitude,
		Longitude:           obj.Longitude,
		Name:                obj.Name,
		OwnerID:             obj.OwnerID,
		Penalty:             obj.Penalty,
		Ref:                 obj.Ref,
		Region:              obj.Region,
		ReportedAdjustments: obj.ReportedAdjustments,
		ReportedDeductions:  obj.ReportedDeductions,
		ReportedNetRevenue:  obj.ReportedNetRevenue,
		ReportedRate:        obj.ReportedRate,
		ReportedRevenue:     obj.ReportedRevenue,
		RevenueBase:         obj.RevenueBase,
		RevenueNet:          obj.RevenueNet,
		RevenueNotTaxable:   obj.RevenueNotTaxable,
		SGC:                 obj.SGC,
		Status:              obj.Status,
		Subtotal:            obj.Subtotal,
		TaxInstances:        taxInstances,
		TemplateID:          obj.TemplateID,
		TotalAmount:         obj.TotalAmount,
		TotalArea:           obj.TotalArea,
		UnitBase:            obj.UnitBase,
		WaterArea:           obj.WaterArea,
	}
}
