package crawler

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

	"github.com/maximotejeda/us_dop_scrapper/helpers"
	"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 {
	client ports.DollarPort
}

func NewBPD(client ports.DollarPort) ports.APIPorts {
	return &bpd{
		client: client,
	}
}

// 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

	uri := os.Getenv("BPD")
	log = log.With("scrapper", "bpd")
	if _, err := page.Goto(uri, 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()
}

func (bp bpd) ExecParser(
	ctx context.Context,
	browser *playwright.Browser,
	log *slog.Logger) (err error) {
	t := true
	ua := helpers.NewMobileUA()
	b := *browser
	page, err := b.NewPage(playwright.BrowserNewPageOptions{
		UserAgent: &ua,
		//	IsMobile:  &t,
		HasTouch: &t,
		Viewport: &playwright.Size{
			Width:  412,
			Height: 915,
		},
		Screen: &playwright.Size{
			Width:  412,
			Height: 915,
		},
	})
	if err != nil {
		log.Error("creating page", "error", err)
		os.Exit(1)
	}
	ctx, cancel := context.WithTimeout(ctx, 6*time.Minute)
	defer page.Close()
	defer cancel()
	inst, err := bp.Scrape(ctx, page, log)
	if err != nil {
		return err
	}
	bp.client.NewHistory(inst[0])
	return err
}
