package regras

import (
	"fmt"
	"strings"
)

// URLsFotos configuração de bases de URL.
type URLsFotos struct {
	MarcaDaguaBase string
	FotoCaixaBase  string
	ArquivosBase   string
}

// FotoItem uma foto resolvida.
type FotoItem struct {
	URL       string
	Principal bool
}

func MontarURLFotoImovel(baseArquivos string, idMaster, idImovel int, nomeFoto string) string {
	nomeFoto = strings.TrimLeft(nomeFoto, "/")
	if strings.HasPrefix(nomeFoto, "http://") || strings.HasPrefix(nomeFoto, "https://") {
		return nomeFoto
	}
	rel := fmt.Sprintf("%d/imoveis/%d/%s", idMaster, idImovel, nomeFoto)
	return strings.TrimRight(baseArquivos, "/") + "/" + rel
}

func MontarURLMarcaDagua(base string, idMaster, idFoto int) string {
	return fmt.Sprintf("%s/marca_dagua/%d/%d.jpg", strings.TrimRight(base, "/"), idMaster, idFoto)
}

func MontarURLFotoCaixa(base string, idFoto int) string {
	return fmt.Sprintf("%s/%d.jpg", strings.TrimRight(base, "/"), idFoto)
}

// ListarFotosVivareal1 XML Media items VivaReal.
func ListarFotosVivareal1(fotos []FotoItem) string {
	var b strings.Builder
	for i, f := range fotos {
		primary := ""
		if i == 0 {
			primary = ` primary="true"`
		}
		b.WriteString(fmt.Sprintf(`<Item medium="image" caption="img%d"%s>%s</Item>`, i, primary, f.URL))
	}
	return b.String()
}

// ListarFotosZap XML Fotos Zap.
func ListarFotosZap(fotos []FotoItem) string {
	var b strings.Builder
	for i, f := range fotos {
		principal := "<Principal>0</Principal>"
		if i == 0 {
			principal = "<Principal>1</Principal>"
		}
		b.WriteString(fmt.Sprintf("<Foto><URLArquivo>%s</URLArquivo>%s</Foto>", f.URL, principal))
	}
	return b.String()
}

// ListarFotosAbcimovel formato ABC.
func ListarFotosAbcimovel(fotos []FotoItem) string {
	var b strings.Builder
	for i, f := range fotos {
		b.WriteString(fmt.Sprintf("<Foto><NomeArquivo>Foto %d</NomeArquivo><URLArquivo>%s</URLArquivo></Foto>", i, f.URL))
	}
	return b.String()
}

// ListarFotosImovelWeb XML ImovelWeb.
func ListarFotosImovelWeb(fotos []FotoItem) string {
	var b strings.Builder
	for i, f := range fotos {
		principal := "<Principal>0</Principal>"
		if i == 0 {
			principal = "<Principal>1</Principal>"
		}
		b.WriteString(fmt.Sprintf("<Foto><NomeArquivo>Photo_%d</NomeArquivo><URLArquivo><![CDATA[%s]]></URLArquivo>%s</Foto>", i, f.URL, principal))
	}
	return b.String()
}

// ListarFotosOLX formato OLX.
func ListarFotosOLX(fotos []FotoItem) string {
	var b strings.Builder
	for i, f := range fotos {
		b.WriteString(fmt.Sprintf("<NomeArquivo>Photo_%d</NomeArquivo><URLArquivo><![CDATA[%s]]></URLArquivo>", i, f.URL))
	}
	return b.String()
}

// ListarFotosMercadolivre formato ML.
func ListarFotosMercadolivre(fotos []FotoItem) string {
	var b strings.Builder
	for i, f := range fotos {
		b.WriteString(fmt.Sprintf("<picture><url>%s</url></picture>", f.URL))
		if i == 0 {
			_ = i
		}
	}
	return b.String()
}

// FormatoFotosPorPortal escolhe formatador.
type FormatoFotos int

const (
	FormatoVivareal FormatoFotos = iota
	FormatoZap
	FormatoAbc
	FormatoImovelWeb
	FormatoOLX
	FormatoMercadoLivre
)

func RenderizarFotos(formato FormatoFotos, fotos []FotoItem) string {
	switch formato {
	case FormatoZap:
		return ListarFotosZap(fotos)
	case FormatoAbc:
		return ListarFotosAbcimovel(fotos)
	case FormatoImovelWeb:
		return ListarFotosImovelWeb(fotos)
	case FormatoOLX:
		return ListarFotosOLX(fotos)
	case FormatoMercadoLivre:
		return ListarFotosMercadolivre(fotos)
	default:
		return ListarFotosVivareal1(fotos)
	}
}

// MesclarFotosPadraoJSON mescla fotos do imóvel com URLs do JSON fotos_padrao.
func MesclarFotosPadraoJSON(fotosImovel []FotoItem, urlsPadrao []string, soHTTP bool) []FotoItem {
	out := make([]FotoItem, 0, len(fotosImovel)+len(urlsPadrao))
	out = append(out, fotosImovel...)
	for _, u := range urlsPadrao {
		if soHTTP && !strings.HasPrefix(u, "http") {
			continue
		}
		out = append(out, FotoItem{URL: u})
	}
	return out
}
