package app

import (
	"encoding/base64"
	"html/template"

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

// UnMarshalCoordinate converts swagger to enriched first class object
func UnMarshalCoordinate(s *geo_models.Coordinate, principal *User) *Coordinate {
	taxTypes := []*TaxType{}
	for _, itm := range s.TaxTypes {
		if itm.ID != "" {
			taxTypes = append(taxTypes, UnMarshalTaxType(itm))
		}
	}
	var situs string
	if s.IsDistrict {
		situs = s.State + " / " + s.County + " / " + s.Place
	} else {
		situs = s.State + " / " + s.County
	}
	return &Coordinate{
		ID:               s.ID,
		BusinessTaxTypes: GetBusinessTaxTypes(taxTypes),
		CannabisTypes:    GetCannabisTaxTypes(taxTypes),
		Country:          s.Country,
		CountryID:        s.CountryID,
		County:           s.County,
		CountyID:         s.CountyID,
		ExciseTaxTypes:   GetExciseTaxTypes(taxTypes),
		Focus:            s.Focus,
		FormattedAddress: s.FormattedAddress,
		IsDistrict:       s.IsDistrict,
		Latitude:         s.Latitude,
		Longitude:        s.Longitude,
		Map:              template.URL(base64.RawStdEncoding.EncodeToString(s.Map)), //nolint:gosec // it definitely is
		MerchTaxTypes:    GetMerchTaxTypes(taxTypes),
		Name:             s.Name,
		Neighborhood:     s.Neighborhood,
		Place:            s.Place,
		Geocode:          s.PlaceGeocode,
		PlaceID:          s.PlaceID,
		PostalCode:       s.PostalCode,
		Ref:              s.Ref,
		Situs:            situs,
		State:            s.State,
		StateID:          s.StateID,
		Status:           s.Status,
		Street:           s.Street,
		StreetNumber:     s.StreetNumber,
		StreetView:       template.URL(base64.RawStdEncoding.EncodeToString(s.StreetView)), //nolint:gosec,lll // it definitely is
		TelecomTaxTypes:  GetTelecomTaxTypes(taxTypes),
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *Coordinate) MarshalToSwagger() *geo_models.Coordinate {
	theTaxTypes := []*geo_models.TaxType{}
	if obj.TaxTypes != nil {
		for _, itm := range obj.TaxTypes {
			theTaxTypes = append(theTaxTypes, itm.MarshalToSwagger())
		}
	}
	var taxRate geo_models.TaxRate
	if obj.TaxRate != nil {
		taxRate = geo_models.TaxRate{
			CombinedRate: obj.TaxRate.CombinedRate,
			County:       obj.TaxRate.County,
			CountyID:     obj.TaxRate.CountyID,
			CountyRate:   obj.TaxRate.CountyRate,
			Date:         obj.TaxRate.Date,
		}
	}
	return &geo_models.Coordinate{
		ID:               obj.ID,
		Country:          obj.Country,
		CountryID:        obj.CountryID,
		County:           obj.County,
		CountyID:         obj.CountyID,
		Focus:            obj.Focus,
		FormattedAddress: obj.FormattedAddress,
		IsDistrict:       obj.IsDistrict,
		Latitude:         obj.Latitude,
		Longitude:        obj.Longitude,
		//	Map:              obj.Map,
		Name:         obj.Name,
		Neighborhood: obj.Neighborhood,
		Place:        obj.Place,
		PlaceGeocode: obj.Geocode,
		PlaceID:      obj.PlaceID,
		PostalCode:   obj.PostalCode,
		Ref:          obj.Ref,
		State:        obj.State,
		StateID:      obj.StateID,
		Status:       obj.Status,
		Street:       obj.Street,
		StreetNumber: obj.StreetNumber,
		//	StreetView:       obj.StreetView,
		TaxTypes: theTaxTypes,
		TaxRate:  &taxRate,
	}
}
