package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/regs/regs_models"
	"github.com/google/uuid"
)

// UnMarshalFilingType decodes swagger to first class object
func UnMarshalFilingType(s *regs_models.FilingType) *FilingType {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	var instances []*FilingTypeInstance
	var dueDates []*FilingScheduleItem
	if s.Instances != nil {
		instances = []*FilingTypeInstance{}
		for _, itm := range s.Instances {
			instances = append(instances, &FilingTypeInstance{
				CountryID:    itm.CountryID,
				CountyID:     itm.CountyID,
				FilingTypeID: itm.FilingTypeID,
				ObjectType:   itm.ObjectType,
				PlaceID:      itm.PlaceID,
				StateID:      itm.StateID,
			})
		}
		for _, itm := range s.DueDates {
			dueDates = []*FilingScheduleItem{}
			dueDate, e2 := time.Parse(dateTimeFormat, itm.DueDate)
			if e2 != nil {
				dueDate, e2 = time.Parse(dateFormatAlt, itm.DueDate)
			}
			dueDates = append(dueDates, &FilingScheduleItem{
				Description: itm.Description,
				DueDate: sql.NullTime{
					Time:  dueDate,
					Valid: e2 == nil,
				},
			})
		}
	}
	return &FilingType{
		AccountID:              s.AccountID,
		ContactID:              s.ContactID,
		CreatedByID:            s.CreatedByID,
		DueDates:               dueDates,
		FilingCity:             s.FilingCity,
		FilingCountry:          s.FilingCountry,
		FilingPostalCode:       s.FilingPostalCode,
		FilingState:            s.FilingState,
		FilingStreet:           s.FilingStreet,
		FormName:               s.FormName,
		FormVersion:            s.FormVersion,
		Frequency:              s.Frequency,
		FullName:               s.FullName,
		ID:                     s.ID,
		Instances:              instances,
		LastModifiedByID:       s.LastModifiedByID,
		Level:                  s.Level,
		Name:                   s.Name,
		OwnerID:                s.OwnerID,
		SagaType:               s.SagaType,
		SubmissionMethod:       s.SubmissionMethod,
		TemplateInstructionsID: s.TemplateInstructionsID,
		TemplateReturnID:       s.TemplateReturnID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
	}
}

// MarshalToSwagger encodes first class object
func (obj *FilingType) MarshalToSwagger() *regs_models.FilingType {
	var instances []*regs_models.FilingTypeInstance
	if obj.Instances != nil {
		instances = []*regs_models.FilingTypeInstance{}
		for _, itm := range obj.Instances {
			instances = append(instances, &regs_models.FilingTypeInstance{
				CountryID:    itm.CountryID,
				CountyID:     itm.CountyID,
				FilingTypeID: itm.FilingTypeID,
				ObjectType:   itm.ObjectType,
				PlaceID:      itm.PlaceID,
				StateID:      itm.StateID,
			})
		}
	}
	var dueDates []*regs_models.FilingScheduleItem
	if obj.DueDates != nil {
		dueDates = []*regs_models.FilingScheduleItem{}
		for _, itm := range obj.DueDates {
			dueDates = append(dueDates, &regs_models.FilingScheduleItem{
				DueDate:     itm.DueDate.Time.Format(dateFormat),
				Description: itm.Description,
			})
		}
	}
	return &regs_models.FilingType{
		ID:                     obj.ID,
		AccountID:              obj.AccountID,
		ContactID:              obj.ContactID,
		CreatedByID:            obj.CreatedByID,
		CreatedDate:            obj.CreatedDate.Time.Format(dateTimeFormat),
		DueDates:               dueDates,
		FilingCity:             obj.FilingCity,
		FilingCountry:          obj.FilingCountry,
		FilingPostalCode:       obj.FilingPostalCode,
		FilingState:            obj.FilingState,
		FilingStreet:           obj.FilingStreet,
		FormName:               obj.FormName,
		FormVersion:            obj.FormVersion,
		Frequency:              obj.Frequency,
		FullName:               obj.FullName,
		Instances:              instances,
		LastModifiedByID:       obj.LastModifiedByID,
		LastModifiedDate:       obj.LastModifiedDate.Time.Format(dateTimeFormat),
		Level:                  obj.Level,
		Name:                   obj.Name,
		OwnerID:                obj.OwnerID,
		SagaType:               obj.SagaType,
		SubmissionMethod:       obj.SubmissionMethod,
		TemplateInstructionsID: obj.TemplateInstructionsID,
		TemplateReturnID:       obj.TemplateReturnID,
	}
}
