package crawler

import (
	"context"
	"fmt"
	"log/slog"
	"os"
	"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 bdr struct {
	client ports.DollarPort
}

func NewBDR(client ports.DollarPort) ports.APIPorts {
	return &bdr{
		client: client,
	}
}
func (bd bdr) Scrape(ctx context.Context, page playwright.Page, log *slog.Logger) (insts []*domain.History, err error) {
	tout := 120000.00
	log = log.With("scrapper", "bdr")
	uri := os.Getenv("BDR")
	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
	}

	err = page.WaitForLoadState()
	if err != nil {
		log.Error("waiting for page state", "err", err)
		return nil, err
	}

	page.Locator("section#divisas").WaitFor()
	compraLocator := page.Locator("span#compraUS")
	compraLocator.WaitFor()
	ventaLocator := page.Locator("span#ventaUS")
	compraSTR, err := compraLocator.TextContent()
	if err != nil {
		log.Error("parsing compra", "err", err)
		return nil, err
	}
	ventaSTR, err := ventaLocator.TextContent()
	if err != nil {
		log.Error("parsing compra", "err", err)
		return nil, err
	}
	inst := &domain.History{
		Name:   "banreservas",
		Parser: "brd",
		Parsed: time.Now().Unix(),
	}

	compra := helpers.Normalize(compraSTR)
	venta := helpers.Normalize(ventaSTR)

	inst.Compra = compra
	inst.Venta = venta

	if inst.Compra == 0 || inst.Venta == 0 {
		return nil, fmt.Errorf("brd: institution not parsed")
	}
	insts = append(insts, inst)
	return insts, nil
}

func (bd bdr) 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()
	insts, err := bd.Scrape(ctx, page, log)
	if err != nil {
		return err
	}
	err = bd.client.NewHistory(insts[0])

	return err
}
