package app

import (
	"database/sql"
	"reflect"
	"time"

	"code.tnxs.net/taxnexus/lib/api/crm/crm_models"
	"github.com/google/uuid"
)

// ReMarshalCompany converts microservice Models object to swagger
func ReMarshalCompany(p interface{}) crm_models.Company {
	// reflect input and set to crm_models type
	t := reflect.ValueOf(&p).Elem()
	s := crm_models.Company{}
	r := reflect.ValueOf(&s).Elem()
	for i := 0; i < t.NumField(); i++ {
		r.Field(i).Set(t.Field(i))
	}
	return s
}

// UnMarshalCompany decodes swagger to first class object
func UnMarshalCompany(s *crm_models.Company) *Company {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	dateClosed, e2 := time.Parse(dateTimeFormat, s.DateClosed)
	return &Company{
		ID:                  s.ID,
		AccountID:           s.AccountID,
		AccountNumberPrefix: s.AccountNumberPrefix,
		BillingAddress:      UnMarshalCrmAddress(s.BillingAddress),
		BillingAdvice:       s.BillingAdvice,
		BillingContactID:    s.BillingContactID,
		BillingEmail:        s.BillingEmail,
		BillingPhone:        s.BillingPhone,
		BillingWebsite:      s.BillingWebsite,
		CoaTemplateID:       s.COATemplateID,
		ColorAccent1:        s.ColorAccent1,
		ColorAccent2:        s.ColorAccent2,
		ColorPrimary:        s.ColorPrimary,
		CreatedByID:         s.CreatedByID,
		CustomerSuccessID:   s.CustomerSuccessID,
		DefaultAddress:      UnMarshalCrmAddress(s.DefaultAddress),
		DefaultCompany:      s.DefaultCompany,
		FontBody:            s.FontBody,
		FontHeading:         s.FontBody,
		FontHeadingNarrow:   s.FontHeadingNarrow,
		FontLink:            s.FontLink,
		FontMono:            s.FontMono,
		International:       s.International,
		LastAccountNumber:   s.LastAccountNumber,
		LastModifiedByID:    s.LastModifiedByID,
		LastTaxtypeNumber:   s.LastTaxTypeNumber,
		Logo:                s.Logo,
		Name:                s.Name,
		OwnerID:             s.OwnerID,
		PreparerID:          s.PreparerID,
		PricebookID:         s.PricebookID,
		TenantID:            s.TenantID,
		UserTechLeadID:      s.UserTechLeadID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		DateClosed: sql.NullTime{
			Time:  dateClosed,
			Valid: e2 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *Company) MarshalToSwagger() *crm_models.Company {
	return &crm_models.Company{
		ID:                  obj.ID,
		AccountID:           obj.AccountID,
		AccountNumberPrefix: obj.AccountNumberPrefix,
		BillingAddress:      obj.BillingAddress.MarshalToCrm(),
		BillingAdvice:       obj.BillingAdvice,
		BillingContactID:    obj.BillingContactID,
		BillingEmail:        obj.BillingEmail,
		BillingPhone:        obj.BillingPhone,
		BillingWebsite:      obj.BillingWebsite,
		COATemplateID:       obj.CoaTemplateID,
		ColorAccent1:        obj.ColorAccent1,
		ColorAccent2:        obj.ColorAccent2,
		ColorPrimary:        obj.ColorPrimary,
		CreatedByID:         obj.CreatedByID,
		CreatedDate:         obj.CreatedDate.Time.Format(dateTimeFormat),
		CustomerSuccessID:   obj.CustomerSuccessID,
		DateClosed:          obj.DateClosed.Time.Format(dateTimeFormat),
		DefaultAddress:      obj.DefaultAddress.MarshalToCrm(),
		DefaultCompany:      obj.DefaultCompany,
		FontBody:            obj.FontBody,
		FontHeading:         obj.FontHeading,
		FontHeadingNarrow:   obj.FontHeadingNarrow,
		FontLink:            obj.FontLink,
		FontMono:            obj.FontMono,
		International:       obj.International,
		LastAccountNumber:   obj.LastAccountNumber,
		LastModifiedByID:    obj.LastModifiedByID,
		LastModifiedDate:    obj.LastModifiedDate.Time.Format(dateTimeFormat),
		LastTaxTypeNumber:   obj.LastTaxtypeNumber,
		Logo:                obj.Logo,
		Name:                obj.Name,
		OwnerID:             obj.OwnerID,
		PreparerID:          obj.PreparerID,
		PricebookID:         obj.PricebookID,
		TenantID:            obj.TenantID,
		UserTechLeadID:      obj.UserTechLeadID,
	}
}
