package main

import (
	"encoding/json"
	"net/http"
	"os"
	"testing"
	"time"

	twitterscraper "github.com/imperatrona/twitter-scraper"
)

/*
 {"Avatar":"https://pbs.twimg.com/profile_images/1812708680594296832/TkKsEyvW_normal.jpg","Banner":"https://pbs.twimg.com/profile_banners/1783145144700874752/1721029871","Biography":"#AON: The best way to quickly build and share Web3 #Al Apps\n\nJoin our community: https://t.co/ECV29IujSK","Birthday":"","FollowersCount":19697,"FollowingCount":72,"FriendsCount":37,"IsPrivate":false,"IsVerified":false,"Joined":"2024-04-24T14:45:19Z","LikesCount":72,"ListedCount":3,"Location":"Hong Kong ","Name":"AON","PinnedTweetIDs":["1800805503066661056"],"TweetsCount":129,"URL":"https://twitter.com/aon_aonet","UserID":"1783145144700874752","Username":"aon_aonet","Website":"https://aonnet.io/","Sensitive":false,"Following":false,"FollowedBy":false}

*/

func TestProfile(t *testing.T) {

	scraper := twitterscraper.New()

	i := 0

	for {

		i = i + 1

		t.Log(i, time.Now())

		pro, err := scraper.GetProfile("aon_aonet")

		if err != nil {
			t.Fatal(err)
		}

		proAsJson, err := json.Marshal(pro)

		if err != nil {
			t.Fatal(err)
		}

		t.Log(string(proAsJson))

		t.Log(pro.FollowersCount)

		time.Sleep(time.Second)
	}

}

func TestAccounts(t *testing.T) {

	accounts, err := GetAvailableAccounts()

	if err != nil {
		t.Fatal(err)
	}

	for k, v := range accounts {
		t.Log(k, v)
	}

}

// func TestSetCookies(t *testing.T) {

// 	accounts, err := GetAvailableAccounts()

// 	if err != nil {
// 		t.Fatal(err)
// 	}

// 	// for k, v := range accounts {
// 	// 	t.Log(k, v)
// 	// }

// 	data, err := json.Marshal(accounts)

// 	if err != nil {
// 		t.Fatal(err)
// 	}

// 	for _, v := range accounts {
// 		//t.Log(k, v)
// 		if err := UpdateCookies(v.Username, data); err != nil {
// 			t.Fatal(err)
// 		}
// 	}
// }

func TestGetLoginAccount(t *testing.T) {

	accounts, err := GetLoginAccount()

	if err != nil {
		t.Fatal(err)
	}

	t.Log("login account ok")

	for k, v := range accounts {
		//v.Timer = time.NewTimer(time.Duration(k) * time.Duration(5) * time.Minute)
		accChan <- ScraperWithTimer{
			Scraper: v,
			Timer:   time.NewTimer(time.Duration(k) * time.Duration(5) * time.Minute),
		}
	}

	for {
		select {
		case account := <-accChan:

			<-account.Timer.C

			users, _, err := account.WithDelay(300).FetchFollowers("bitcoin", 20, "")
			if err != nil {
				t.Log(err.Error())
				continue
			}

			t.Log("len(users)", len(users), time.Now())

			account.Timer = time.NewTimer(time.Hour)
			accChan <- account

		}

	}
}

func TestCookies(t *testing.T) {

	scraper := twitterscraper.New()

	var cookies []*http.Cookie
	f, _ := os.Open("16.json")
	if err := json.NewDecoder(f).Decode(&cookies); err != nil {
		t.Fatal(err)
	}

	t.Log("len(cookies)", len(cookies))

	scraper.SetCookies(cookies)
	if !scraper.IsLoggedIn() {
		panic("Invalid cookies")
	}

	// users, _, err := scraper.FetchFollowers("bitcoin", 20, "")
	// if err != nil {
	// 	t.Log(err.Error())
	// 	continue
	// }

}
