package bpd

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

	"github.com/maximotejeda/us_dop_db/db"
	"github.com/maximotejeda/us_dop_scrapper/helpers"

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

var (
	uri = os.Getenv("BPD")
)

// Scrape
// needs a mobile User Agent
func Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (inst *db.History, err error) {
	tout := 120000.00
	//start := time.Now()
	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 = &db.History{
		Name:   "banco popular",
		Parser: "bpd",
		Parsed: time.Now().UTC(),
	}
	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 inst, nil
}

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

func ExecParser(
	ctx context.Context,
	db *db.DB,
	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 := Scrape(ctx, page, log)
	if err != nil {
		return err
	}
	err = db.Inspect(*inst)
	return err
}
