package plist

import (
	"bytes"
	"encoding/gob"
	"io/ioutil"
	"log"
	"os"
	"testing"

	"github.com/Pallinder/go-randomdata"
	"github.com/emirpasic/gods/utils"
)

const CompartorSize = 100
const NumberMax = 50000000

func Save(t *testing.T) {

	f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
	if err != nil {
		log.Println(err)
	}

	//fmt.Println(userBytes)

	var l []int

	// for i := 0; len(l) < 1000; i++ {
	// 	v := randomdata.Number(0, 65535)
	// 	l = append(l, v)
	// }

	//m := make(map[int]int)
	for i := 0; len(l) < CompartorSize; i++ {
		v := randomdata.Number(0, NumberMax)
		// if _, ok := m[v]; !ok {
		// 	m[v] = v
		l = append(l, v)
		// }
	}

	var result bytes.Buffer
	encoder := gob.NewEncoder(&result)
	encoder.Encode(l)
	lbytes := result.Bytes()
	f.Write(lbytes)

}

func loadTestData() []int {
	data, err := ioutil.ReadFile("../l.log")
	if err != nil {
		log.Println(err)
	}
	var l []int
	decoder := gob.NewDecoder(bytes.NewReader(data))
	decoder.Decode(&l)
	return l
}

func TestInsert(t *testing.T) {
	pl := New(utils.IntComparator)
	for i := 0; i < 10; i++ {
		pl.Push(i)
	}

	if pl.size != 10 {
		t.Error(pl.size)
	}

	if pl.String() != "9 8 7 6 5 4 3 2 1 0" {
		t.Error(pl.String())
	}

	if pl.RString() != "0 1 2 3 4 5 6 7 8 9" {
		t.Error(pl.RString())
	}

	for i := 0; i < 10; i++ {
		pl.Push(i)
	}

	if pl.String() != "9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0" {
		t.Error(pl.String())
	}

	if pl.RString() != "0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9" {
		t.Error(pl.RString())
	}
}

func TestIterator(t *testing.T) {
	pl := New(utils.IntComparator)
	for i := 0; i < 10; i++ {
		pl.Push(i)
	}

	iter := pl.Iterator()

	for i := 0; iter.Next(); i++ {
		if iter.Value() != 9-i {
			t.Error("iter.Next() ", iter.Value(), "is not equal ", 9-i)
		}
	}

	if iter.cur != iter.pl.tail {
		t.Error("current point is not equal tail ", iter.pl.tail)
	}

	for i := 0; iter.Prev(); i++ {
		if iter.Value() != i {
			t.Error("iter.Prev() ", iter.Value(), "is not equal ", i)
		}
	}
}

func TestCircularIterator(t *testing.T) {
	pl := New(utils.IntComparator)
	for i := 0; i < 10; i++ {
		pl.Push(i)
	}

	iter := pl.CircularIterator()

	for i := 0; i != 10; i++ {
		iter.Next()
		if iter.Value() != 9-i {
			t.Error("iter.Next() ", iter.Value(), "is not equal ", 9-i)
		}
	}

	if iter.cur != iter.pl.tail.prev {
		t.Error("current point is not equal tail ", iter.pl.tail.prev)
	}

	if iter.Next() {
		if iter.Value() != 9 {
			t.Error("iter.Value() != ", iter.Value())
		}
	}

	iter.MoveToTail()
	for i := 0; i != 10; i++ {
		iter.Prev()
		if iter.Value() != i {
			t.Error("iter.Prev() ", iter.Value(), "is not equal ", i)
		}
	}

	if iter.cur != iter.pl.head.next {
		t.Error("current point is not equal tail ", iter.pl.tail.prev)
	}

	if iter.Prev() {
		if iter.Value() != 0 {
			t.Error("iter.Value() != ", iter.Value())
		}
	}
}

func TestGet(t *testing.T) {
	pl := New(utils.IntComparator)
	for i := 0; i < 10; i++ {
		pl.Push(i)
	}

	for _, v := range []int{0, 9, 5, 7} {
		if pl.Get(v) != (9 - v) {
			t.Error(v, "Get == ", pl.Get(v))
		}
	}

	func() {
		defer func() {
			if err := recover(); err == nil {
				t.Error("out index, but is not error")
			}
		}()

		pl.Get(10)
	}()
}

func TestRemove(t *testing.T) {
	pl := New(utils.IntComparator)
	for i := 0; i < 10; i++ {
		pl.Push(i)
	}

	pl.RemoveWithIndex(0)
	if pl.Get(0).(int) != 8 {
		t.Error(pl.Get(0))
	}

	pl.RemoveWithIndex(-1)
	if pl.Get(-1).(int) != 1 {
		t.Error(pl.Get(-1))
	}

}

func BenchmarkGet(b *testing.B) {
	pl := New(utils.IntComparator)

	b.N = 100

	for i := 0; i < b.N; i++ {
		v := randomdata.Number(0, 65535)
		pl.Push(v)
	}

	b.ResetTimer()
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		if i%2 == 0 {
			pl.Get(i)
		}
	}

}
func BenchmarkInsert(b *testing.B) {

	l := loadTestData()

	b.ResetTimer()
	b.StartTimer()

	execCount := 500
	b.N = len(l) * execCount
	for i := 0; i < execCount; i++ {
		pl := New(utils.IntComparator)
		for _, v := range l {
			pl.Push(v)
		}
	}
}