package crawler

import (
	"context"
	"fmt"
	"log/slog"
	"strconv"
	"time"

	"github.com/maximotejeda/us_dop_scrapper/config"
	"github.com/maximotejeda/us_dop_scrapper/internal/application/core/domain"
	"github.com/maximotejeda/us_dop_scrapper/internal/ports"

	"github.com/playwright-community/playwright-go"
)

type bpd struct{}

func NewBPD() ports.APIPorts {
	return &bpd{}
}

// Scrape
// needs a mobile User Agent
func (bp bpd) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
	tout := 120000.00
	log = log.With("scrapper", "bpd")
	if _, err := page.Goto(config.GetBPDURL(), playwright.PageGotoOptions{
		Timeout:   &tout,
		WaitUntil: playwright.WaitUntilStateLoad,
	}); err != nil {
		log.Error("could not get info", "error", err)
		return nil, err
	}
	//log.Info("Page loaded", "time", time.Since(start).Seconds())
	// menu := page.Locator(".footer_est_menu_bpd > li:nth-child(3)")
	container := page.Locator("div.tasa.tasa_dolar")
	container.WaitFor()
	compraInput := page.Locator("input#compra_peso_dolar")
	compraInput.WaitFor(playwright.LocatorWaitForOptions{
		Timeout: &tout,
		State:   playwright.WaitForSelectorStateVisible,
	})

	ventaInput := page.Locator("input#venta_peso_dolar")
	ventaInput.WaitFor()
	compraSTR, err := compraInput.InputValue()
	if err != nil {
		log.Error("compra value", "err", err)
		return nil, err
	}
	// log.Info("Compra get value", "time", time.Since(start).Seconds())
	ventaSTR, err := ventaInput.InputValue()
	if err != nil {
		log.Error("compra value", "err", err)
		return nil, err
	}
	inst := &domain.History{
		Name:   "banco popular",
		Parser: "bpd",
		Parsed: time.Now().Unix(),
	}
	compra, err := strconv.ParseFloat(compraSTR, 64)
	if err != nil {
		log.Error("parsing value", "where", "compra", "err", err)
		return nil, err
	}
	venta, err := strconv.ParseFloat(ventaSTR, 64)
	if err != nil {
		log.Error("parsing value", "where", "venta", "err", err)
		return nil, err
	}
	inst.Compra = compra
	inst.Venta = venta

	if inst.Compra == 0 || inst.Venta == 0 {
		return nil, fmt.Errorf("bpd: institution not parsed")
	}
	return []*domain.History{inst}, nil
}

func HoverTasas(page playwright.Page) {
	tasasMenu := page.Locator(".footer_est_menu_bpd > li:nth-child(3)")
	tasasMenu.Hover()
}
