package tmobile

import (
	"context"
	"sync"
	"testing"
	"time"

	"github.com/fiskerinc/cloud-services/pkg/grpc/sms"
)

var CLIENT_ID = ""
var SECRET = ""
var PRIVATE_KEY = ""

func TestTMobileSendMessage(t *testing.T) {
	t.Skip()
	tg, err := InitTokenGen(
		PRIVATE_KEY,
		CLIENT_ID,
		SECRET,
	)
	if err != nil {
		t.Error(err)
	}

	client, err := NewTMobileClient(Endpoint, tg, time.Second*450)
	if err != nil {
		t.Error(err)
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()

	o, err := client.AccessToken(context.Background())
	if err != nil {
		t.Error(err)
	}

	client.SetAccessToken(o.AccessToken)

	msg := SendSMSRequest{
		ICCID:       "8901882000784166054",
		MessageText: "local_test",
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()

	out, err := client.SendSMS(ctx, &msg)
	if err != nil {
		t.Error(err)
		return
	}
	t.Error(out.SmsMessageID)
}

func TestTMobileCheckMessageStatus(t *testing.T) {
	t.Skip()
	messageID := "473007039"
	tg, err := InitTokenGen(
		PRIVATE_KEY,
		CLIENT_ID,
		SECRET,
	)
	if err != nil {
		t.Error(err)
	}

	tmobc, err := NewTMobileClient(
		Endpoint,
		tg, time.Minute*2)
	if err != nil {
		t.Error(err)
	}

	o, err := tmobc.AccessToken(context.Background())
	if err != nil {
		t.Error(err)
	}

	tmobc.SetAccessToken(o.AccessToken)

	failed := 0
	r := sync.WaitGroup{}
	for x := 0; x < 5; x++ {
		r.Add(1)
		go func(y int) {
			_, err := tmobc.Details(context.Background(), messageID)
			if err != nil {
				// t.Error(err)
				failed++
				t.Log(y, ",")
			}
			r.Done()
		}(x)
	}
	r.Wait()

	t.Error(failed)
}

func TestTMobileCustomAttributes(t *testing.T) {
	t.Skip()
	tg, err := InitTokenGen(
		PRIVATE_KEY,
		CLIENT_ID,
		SECRET,
	)
	if err != nil {
		t.Error(err)
	}

	client, err := NewTMobileClient(Endpoint, tg, time.Second*450)
	if err != nil {
		t.Error(err)
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()

	o, err := client.AccessToken(context.Background())
	if err != nil {
		t.Error(err)
	}

	client.SetAccessToken(o.AccessToken)

	custumAttributes := CustomAtributesRequest{
		ICCID:          "8901882000784166054",
		AccountCustom1: "US",
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()
	out, err := client.CustomAttributes(ctx, &custumAttributes)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(out.ICCID)
}

func TestTMobileDeviceDetails(t *testing.T) {
	t.Skip()
	tg, err := InitTokenGen(
		PRIVATE_KEY,
		CLIENT_ID,
		SECRET,
	)
	if err != nil {
		t.Error(err)
	}

	client, err := NewTMobileClient(Endpoint, tg, time.Second*450)
	if err != nil {
		t.Error(err)
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()

	o, err := client.AccessToken(context.Background())
	if err != nil {
		t.Error(err)
	}

	client.SetAccessToken(o.AccessToken)

	deviceDetailsRequest := DeviceDetailsRequest{
		ICCID: "8901882000784166054",
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()
	out, err := client.DeviceDetails(ctx, &deviceDetailsRequest)
	if err != nil {
		t.Error(err)
		return
	}

	t.Errorf("%+v", out)
}

func TestTMobileGetProducts(t *testing.T) {
	t.Skip()
	tg, err := InitTokenGen(
		PRIVATE_KEY,
		CLIENT_ID,
		SECRET,
	)
	if err != nil {
		t.Error(err)
	}

	client, err := NewTMobileClient(Endpoint, tg, time.Second*450)
	if err != nil {
		t.Error(err)
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()

	o, err := client.AccessToken(context.Background())
	if err != nil {
		t.Error(err)
	}

	client.SetAccessToken(o.AccessToken)

	getProductsRequest := sms.GetAvailableProductsRequest{
		AccountId:             "500556839",
		ProductType:           []string{"RATEPLAN"},
		ProductClassification: []string{"IOTCONNECTIVITY"},
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()
	out, err := client.GetProducts(ctx, &getProductsRequest)
	if err != nil {
		t.Error(err)
		return
	}
	t.Errorf("%+v", out)
}

func TestTMobileChangeRatePlan(t *testing.T) {
	t.Skip()
	tg, err := InitTokenGen(
		PRIVATE_KEY,
		CLIENT_ID,
		SECRET,
	)
	if err != nil {
		t.Error(err)
	}

	client, err := NewTMobileClient(Endpoint, tg, time.Second*450)
	if err != nil {
		t.Error(err)
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()

	o, err := client.AccessToken(context.Background())
	if err != nil {
		t.Error(err)
	}

	client.SetAccessToken(o.AccessToken)

	changeRatePlan := ChangeRatePlanRequest{
		ICCID:     "8901882000784166054",
		ProductId: "716f8a59-6d2c-4687-b927-990a46b847cc",
		AccountId: "500556839",
	}

	ctx, cancel = context.WithTimeout(context.Background(), time.Second*450)
	defer cancel()
	out, err := client.ChangeRatePlan(ctx, &changeRatePlan)
	if err != nil {
		t.Error(err)
		return
	}
	t.Error(out.ICCID)
}

/* Available rate plans at Tmobile

{id:"64510d144707f865761ff0b0" productId:"03f30679-30a3-4d5c-ad49-be62ec26a886" shortDescription:"Fisker Intl3 5GB" status:"Published" effectiveDate:"2023-10-03T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl3 5GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64510d137953b71c0564d365" productId:"90111353-7f8c-4315-8175-2abe127f2c97" shortDescription:"Fisker Intl3 7GB" status:"Published" effectiveDate:"2023-10-03T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl3 7GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64510d137953b71c0564d366" productId:"71e97975-8b63-4c93-b2e4-60a48914408f" shortDescription:"Fisker Intl3 2GB" status:"Published" effectiveDate:"2023-10-03T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl3 2GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"645056444707f865761ff0ae" productId:"9621b445-c5bd-4ef5-9ee7-415c8c6045b6" shortDescription:"Fisker Intl2 7GB" status:"Published" effectiveDate:"2023-10-03T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl2 7GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64504f37df187e2be859acde" productId:"6f56afbf-dd60-4210-9894-c7f3675f643d" shortDescription:"Fisker Intl2 2GB" status:"Published" effectiveDate:"2023-10-03T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl2 2GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64505643abfe5c3f44b7bb6a" productId:"ee5bb6bb-074d-47ba-b47d-907f95a77e9f" shortDescription:"Fisker Intl2 5GB" status:"Published" effectiveDate:"2023-10-03T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl2 5GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64504830abfe5c3f44b7bb68" productId:"92bf0610-098d-4ead-b38b-c8c4b2a1ede1" shortDescription:"Fisker Intl1 2GB" status:"Published" effectiveDate:"2023-08-21T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl1 2GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64e3a9fe3dc11817f3797090" productId:"716f8a59-6d2c-4687-b927-990a46b847cc" shortDescription:"Fisker USA 7GB" status:"Published" effectiveDate:"2023-08-21T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker USA 7GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64e3a686117310400e90b45f" productId:"af6dc7f2-cb9e-466f-a24a-003da89c9573" shortDescription:"Fisker USA 2GB" status:"Published" effectiveDate:"2023-08-21T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker USA 2GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64e3a684117310400e90b45e" productId:"74719a08-2d1f-44d2-ae76-fec6409cb0e0" shortDescription:"Fisker USA 5GB" status:"Published" effectiveDate:"2023-08-21T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker USA 5GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"aba2be04-df09-42ac-87ed-f5e53652cf9b" productId:"7290a552-cf8d-4a32-b829-ea8c62c83bda" shortDescription:"Fisker Testing 2GB" longDescription:"pub again to push to AHUB" status:"Published" effectiveDate:"2022-12-08T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Testing 2GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"366bc7c0-c5f1-4e74-9748-a10910f42171" productId:"5bd33e0d-350c-45d3-bf97-8b501ac354d7" shortDescription:"Fisker Monaco 7GB" status:"Published" effectiveDate:"2023-05-09T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Monaco 7GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"39284d65-5688-4324-91fb-95b1271efa44" productId:"2248a17c-483f-4732-b9f1-ce0929666464" shortDescription:"Fisker Monaco 5GB" status:"Published" effectiveDate:"2023-05-09T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Monaco 5GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"1ed2d811-ec98-47ac-8338-1fad104f731d" productId:"e7f09cfe-4c80-4e90-92a9-a1fe92621e6e" shortDescription:"Fisker Monaco 2GB" status:"Published" effectiveDate:"2023-05-09T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Monaco 2GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"5ff7d298-53af-46e0-8058-011eaf95e53a" productId:"6eda215c-cd11-4686-b2c7-3fc4ad5f0a1d" shortDescription:"Fisker Intl1 7GB" status:"Published" effectiveDate:"2023-05-09T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl1 7GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}
{id:"64609dda-4620-4057-b1a4-d8a9450ef404" productId:"164917cd-6bdc-44a7-a769-d4889775d6a4" shortDescription:"Fisker Intl1 5GB" status:"Published" effectiveDate:"2023-05-09T00:00:00Z" expirationDate:"9999-12-31T00:00:00Z" ratePlan:{ratePlanName:"Fisker Intl1 5GB" planType:"Monthly - Flexible Pool" planStatus:"Published"}}

*/
