package regras

import (
	"fmt"
	"strings"
)

func TipoTransacaoVivareal(tipo string) string {
	switch tipo {
	case "Venda":
		return "For Sale"
	case "Locação":
		return "For Rent"
	case "Venda - Locação":
		return "Sale/Rent"
	case "Permuta":
		return "For Sale"
	case "Arrendamento", "Temporada", "Oportunidades":
		return "For Rent"
	default:
		return "For Sale"
	}
}

func TipoPrecoVivareal(valor, tipoValor string) string {
	if valor == "" || valor == "0" || valor == "0.00" || valor == "0.0" {
		return ""
	}
	parts := strings.Split(valor, ".")
	novo := parts[0]
	switch tipoValor {
	case "valor_locacao":
		return fmt.Sprintf(`<RentalPrice currency="BRL" period="Monthly">%s</RentalPrice>`, novo)
	case "valor_locacao_diario":
		return fmt.Sprintf(`<RentalPrice currency="BRL" period="Daily">%s</RentalPrice>`, novo)
	case "valor_venda":
		return fmt.Sprintf(`<ListPrice currency="BRL" period="Monthly">%s</ListPrice>`, novo)
	}
	return ""
}

func TipoPrecoAbc(valor, tipoValor string) string {
	if valor == "" || valor == "0" || valor == "0.00" {
		return "0"
	}
	parts := strings.Split(valor, ".")
	return parts[0]
}

func TipoPrecoSimples(valor string) string {
	if valor == "" || valor == "0" || valor == "0.00" {
		return "0"
	}
	parts := strings.Split(valor, ".")
	return parts[0]
}

func TipoPrecoImovelWel(idMaster int, valor, tipoValor, refCaixa string) string {
	if valor == "" || valor == "0" || valor == "0.00" || valor == "0.0" {
		return ""
	}
	novo := strings.Split(valor, ".")[0]
	if tipoValor == "valor_locacao" {
		return "<PrecoLocacao>" + novo + "</PrecoLocacao>"
	}
	if tipoValor == "valor_venda" {
		if refCaixa != "" {
			return "<PrecoLeilao>" + novo + "</PrecoLeilao>"
		}
		if idMaster == 201 {
			return "<Precoleilao>" + novo + "</Precoleilao>"
		}
		return "<PrecoVenda>" + novo + "</PrecoVenda>"
	}
	return ""
}

func ValorIPTUSemCentavos(valor string) string {
	if valor == "" || valor == "0.00" || valor == "0" {
		return ""
	}
	parts := strings.Split(valor, ".")
	return parts[0]
}

func ValorCondominioSemCentavos(valor string) string {
	return ValorIPTUSemCentavos(valor)
}

func MontarServicosVivareal(features []string) string {
	if len(features) == 0 {
		return ""
	}
	var b strings.Builder
	for _, f := range features {
		f = strings.TrimSpace(f)
		if f == "" {
			continue
		}
		b.WriteString("<Feature>")
		b.WriteString(f)
		b.WriteString("</Feature>")
	}
	return b.String()
}

func DisplayEndereco(viewEndereco int) string {
	if viewEndereco == 1 {
		return "All"
	}
	return "Neighborhood"
}

func EnderecoOculto(viewEndereco int, valor string) string {
	if viewEndereco == 0 {
		return ""
	}
	return valor
}
