package regras

import (
	"fmt"
	"strings"
)

func DescricaoAjustar(refCaixa, estado, descricao string) string {
	if len(refCaixa) > 1 {
		ref := refCaixa[1:]
		if ref != "" {
			text := fmt.Sprintf(`.</br><a href="https://venda-imoveis.caixa.gov.br/sistema/detalhe-imovel.asp#" class="" target="_blank" href="https://venda-imoveis.caixa.gov.br/editais/matricula/%s/%s.pdf">Baixar matrícula do imóvel</a></br></br>`, estado, ref)
			textSemQuebra := strings.ReplaceAll(text, "\n", "")
			descricao = strings.ReplaceAll(descricao, text, "")
			_ = textSemQuebra
		}
	}
	return descricao
}

func MontarDescricaoImovel(descricao, descricaoCaixa, descricaoPadrao, textoAdicional string, usouDescricaoPadrao bool) string {
	desc := StripTags(descricao)
	if usouDescricaoPadrao {
		texto := desc + descricaoPadrao + textoAdicional
		return strings.ReplaceAll(texto, "\n", " <br> ")
	}
	texto := desc + StripTags(descricaoCaixa) + descricaoPadrao + textoAdicional
	return strings.ReplaceAll(texto, "\n", " <br> ")
}

// MontarDescricaoComRegrasCaixa espelha Portal_viva_real linhas 194-230.
func MontarDescricaoComRegrasCaixa(
	descricao, descricaoCaixa string,
	descricaoPadraoFlag int,
	refCaixa string,
	textoPadraoAtivo int,
	textoAdicionalMaster, descricaoPadraoCompleta string,
) (textoAnuncio string) {
	descricaoPadrao := ""
	textoAdicional := ""
	if descricaoPadraoFlag == 1 {
		if refCaixa != "" {
			descricaoPadrao = descricaoPadraoCompleta
		} else {
			descricaoPadrao = textoAdicionalMaster
		}
	} else if refCaixa != "" {
		textoAdicional = textoAdicionalMaster
	}
	if descricaoPadrao != "" {
		textoAnuncio = MontarDescricaoImovel(descricao, descricaoCaixa, descricaoPadrao, textoAdicional, true)
	} else {
		textoAnuncio = MontarDescricaoImovel(descricao, descricaoCaixa, "", textoAdicional, false)
	}
	return DescricaoAjustar(refCaixa, "", textoAnuncio)
}

// MontarDescricaoComRegrasCaixaComEstado igual à versão acima mas passa estado para DescricaoAjustar.
func MontarDescricaoComRegrasCaixaComEstado(
	descricao, descricaoCaixa, estado string,
	descricaoPadraoFlag int,
	refCaixa string,
	textoPadraoAtivo int,
	textoAdicionalMaster, descricaoPadraoCompleta string,
) string {
	t := MontarDescricaoComRegrasCaixa(descricao, descricaoCaixa, descricaoPadraoFlag, refCaixa, textoPadraoAtivo, textoAdicionalMaster, descricaoPadraoCompleta)
	if estado != "" {
		return DescricaoAjustar(refCaixa, estado, t)
	}
	return t
}

func descricaoCaixaJaIncluida(descricao, descricaoCaixa string) bool {
	if descricaoCaixa == "" || descricao == "" {
		return false
	}
	tam := len(descricaoCaixa)
	if tam > 100 {
		tam = 100
	}
	trecho := descricaoCaixa[:tam]
	return strings.Contains(descricao, trecho)
}

// MontarDescricaoImovelWebNovo espelha Portal_imovelWebNovo.php (anti-duplicação caixa).
func MontarDescricaoImovelWebNovo(descricao, descricaoCaixa string, descricaoPadraoFlag int, refCaixa, textoAdicionalMaster, descricaoPadraoCompleta string) string {
	desc := StripTags(descricao)
	descCaixa := StripTags(descricaoCaixa)
	jaIncluida := descricaoCaixaJaIncluida(desc, descCaixa)

	var texto string
	if descricaoPadraoFlag == 1 {
		if refCaixa != "" {
			texto = desc + descricaoPadraoCompleta
		} else if !jaIncluida && descCaixa != "" {
			texto = desc + descCaixa + textoAdicionalMaster
		} else {
			texto = desc + textoAdicionalMaster
		}
	} else if !jaIncluida && descCaixa != "" {
		texto = desc + descCaixa
	} else {
		texto = desc
	}
	texto = strings.ReplaceAll(texto, "\n", " <br> ")
	return RemoverDuplicacoesTexto(texto)
}

// MontarDescricaoChaveNaMao espelha Portal_ChaveNaMao.php.
func MontarDescricaoChaveNaMao(descricao, descricaoCaixa, descricaoPadraoExtra string, descricaoPadraoFlag int, refCaixa, textoPadraoCompleta string) string {
	if descricao == "" && refCaixa == "" {
		return strings.ReplaceAll(textoPadraoCompleta, "\n", " <br> ")
	}
	if descricao != "" && refCaixa == "" {
		texto := descricao
		if descricaoPadraoFlag == 1 {
			texto = descricao + descricaoPadraoExtra
		}
		texto = strings.ReplaceAll(texto, "</br>", "****")
		texto = strings.ReplaceAll(texto, "\n", "####")
		texto = strings.ReplaceAll(texto, "****", "<br>")
		texto = strings.ReplaceAll(texto, "####", "<br>")
		return texto
	}
	if descricao != "" && refCaixa != "" {
		desc1 := StripTags(descricao)
		descCaixa := StripTags(descricaoCaixa)
		jaIncluida := descricaoCaixaJaIncluida(desc1, descCaixa)
		var texto string
		if descricaoPadraoFlag == 1 {
			texto = desc1 + textoPadraoCompleta
		} else if !jaIncluida && descCaixa != "" {
			texto = desc1 + descCaixa
		} else {
			texto = desc1
		}
		return strings.ReplaceAll(texto, "\n", " <br> ")
	}
	return StripTags(descricao)
}

// RemoverDuplicacoesTexto espelha Portal_imovelWebNovo::removerDuplicacoesTexto.
func RemoverDuplicacoesTexto(texto string) string {
	if strings.TrimSpace(texto) == "" {
		return texto
	}
	frase := "FORMAS DE PAGAMENTO ACEITAS"
	limpo := StripTags(texto)
	if strings.Count(limpo, frase) <= 1 {
		return texto
	}
	origLimpo := StripTags(texto)
	pos := strings.LastIndex(origLimpo, frase)
	if pos < 0 {
		return texto
	}
	antes := origLimpo[:pos]
	posAnt := strings.LastIndex(antes, frase)
	if posAnt < 0 {
		return texto
	}
	blocoUltimo := origLimpo[pos:]
	blocoPenultimo := origLimpo[posAnt:pos]
	tam := minInt(150, len(blocoUltimo), len(blocoPenultimo))
	if tam < 50 {
		return texto
	}
	iguais := 0
	for i := 0; i < tam; i++ {
		if blocoUltimo[i] == blocoPenultimo[i] {
			iguais++
		}
	}
	if float64(iguais)/float64(tam)*100 > 90 {
		trecho := blocoUltimo
		if len(trecho) > 150 {
			trecho = trecho[:150]
		}
		if strings.Contains(antes, trecho) {
			return strings.TrimSpace(texto[:pos])
		}
	}
	return texto
}

func minInt(a, b, c int) int {
	m := a
	if b < m {
		m = b
	}
	if c < m {
		m = c
	}
	return m
}

func TextoAdicionalFormatado(texto string) string {
	if strings.TrimSpace(texto) == "" {
		return ""
	}
	return " " + StripTags(texto)
}

func DescricaoPadraoCompleta(textoAdicional, descricaoCaixa, replace string) string {
	desc := strings.TrimSpace(textoAdicional) + " " + strings.TrimSpace(descricaoCaixa)
	desc = StripTags(desc)
	return strings.ReplaceAll(desc, "\n", replace)
}
