package encoding

import (
	"bytes"
	"testing"
)

var (
	short8nString   = "Sunshine"
	longNot8nString = "Golden rays play, Chasing night away."
	long8nString    = "Ducks are fucking great, they quacks, O quackers, what the fuck."

	short8nStringEncodedBytes   = []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
	longNot8nStringEncodedBytes = []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
	long8nStringEncodedBytes    = []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
)

// region encode
func TestGSM7EncodeSimpleASCIIString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := short8nString

	expected := short8nStringEncodedBytes
	err := coder.Encode(&input, &buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if !bytes.Equal(buf.Bytes(), expected) {
		t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
	}
}

func TestGSM7EncodeComplexASCIIString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := longNot8nString

	expected := longNot8nStringEncodedBytes
	err := coder.Encode(&input, &buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if !bytes.Equal(buf.Bytes(), expected) {
		t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
	}
}

func TestGSM7EncodeComplex8nASCIIString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := "Ducks are fucking great, they quacks, O quackers, what the fuck."

	expected := long8nStringEncodedBytes
	err := coder.Encode(&input, &buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if !bytes.Equal(buf.Bytes(), expected) {
		t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
	}
}

func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) {
	coder := &GSM7Coder{}
	input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
	buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
	buf.Reset()

	expected := buf.Cap()
	err := coder.Encode(&input, buf)
	actual := buf.Cap()

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if actual != expected {
		t.Errorf("Expected buffer of size %v, but got %v", expected, actual)
	}
}

func TestGSM7EncodeDoesAllocateWhenNecessary(t *testing.T) {
	coder := &GSM7Coder{}
	input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
	var buf bytes.Buffer
	buf.Reset()

	original := buf.Cap()
	err := coder.Encode(&input, &buf)
	modified := buf.Cap()

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if !(modified > original) {
		t.Errorf("Expected buffer to grow but buffer changed from %v to %v", original, modified)
	}
}

func TestGSM7EncodeEmptyString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := ""

	expected := []byte{}
	err := coder.Encode(&input, &buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if !bytes.Equal(buf.Bytes(), expected) {
		t.Errorf("Expected '%v', but got '%v'", expected, buf.Bytes())
	}
}

// region decode
func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := short8nStringEncodedBytes

	expected := short8nString
	buf.Write(input)
	actual, err := coder.Decode(&buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if actual != expected {
		t.Errorf("Expected '%v', but got '%v'", expected, actual)
	}
}

func TestGSM7DecodeComplexASCIIString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := longNot8nStringEncodedBytes

	expected := longNot8nString
	buf.Write(input)
	actual, err := coder.Decode(&buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if actual != expected {
		t.Errorf("Expected '%v', but got '%v'", expected, actual)
	}
}

func TestGSM7DecodeComplex8nASCIIString(t *testing.T) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := long8nStringEncodedBytes

	expected := long8nString
	buf.Write(input)
	actual, err := coder.Decode(&buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if actual != expected {
		t.Errorf("Expected '%v', but got '%v'", expected, actual)
	}
}

func TestGSM7DecodeEmptyString(t *testing.T) {
	coder := &GSM7Coder{}
	buf := bytes.NewBuffer([]byte{})

	expected := ""
	actual, err := coder.Decode(buf)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	if actual != expected {
		t.Errorf("Expected '%v', but got '%v'", expected, actual)
	}
}

// region insertat
func TestInsertAtBeginning(t *testing.T) {
	data := []byte{2, 3, 4, 0}
	InsertAt(&data, 0, 1)
	expected := []byte{1, 2, 3, 4}
	if !bytes.Equal(data, expected) {
		t.Errorf("expected %v, got %v", expected, data)
	}
}

func TestInsertInMiddle(t *testing.T) {
	data := []byte{2, 3, 4, 0}
	InsertAt(&data, 1, 5)
	expected := []byte{2, 5, 3, 4}
	if !bytes.Equal(data, expected) {
		t.Errorf("expected %v, got %v", expected, data)
	}
}

func TestInsertAtEnd(t *testing.T) {
	data := []byte{1, 2, 3, 0}
	InsertAt(&data, 3, 4)
	expected := []byte{1, 2, 3, 4}
	if !bytes.Equal(data, expected) {
		t.Errorf("expected %v, got %v", expected, data)
	}
}

func TestIndexOutOfBounds(t *testing.T) {
	data := []byte{2, 3, 4}
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("The code did not panic")
		}
	}()
	InsertAt(&data, 4, 5)
}

func TestNegativeIndex(t *testing.T) {
	data := []byte{2, 3, 4}
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("The code did not panic")
		}
	}()
	InsertAt(&data, -1, 1)
}

func TestMaintainsOrderAfterInsertion(t *testing.T) {
	data := []byte{2, 3, 4, 0}
	InsertAt(&data, 1, 1)
	expected := []byte{2, 1, 3, 4}
	if !bytes.Equal(data, expected) {
		t.Errorf("expected %v, got %v", expected, data)
	}
}

func TestMaintainsLength(t *testing.T) {
	data := []byte{2, 3, 4, 0}
	InsertAt(&data, 1, 1)
	if len(data) != 4 {
		t.Errorf("expected length 4, got %d", len(data))
	}
}

func TestDeletesLastValue(t *testing.T) {
	data := []byte{2, 3, 4, 5}
	InsertAt(&data, 0, 1)
	expected := []byte{1, 2, 3, 4}
	if !bytes.Equal(data, expected) {
		t.Errorf("expected %v, got %v", expected, data)
	}
}

// region misc tests
func TestGSM7EncodesIntoSmallString(t *testing.T) {
	input := short8nString
	expected := 7
	actual := GSM7Coder{}.EncodesInto(&input)
	if actual != expected {
		t.Errorf("Expected %d, but got %d", expected, actual)
	}
}

func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
	input := longNot8nString
	expected := 33
	actual := GSM7Coder{}.EncodesInto(&input)
	if actual != expected {
		t.Errorf("Expected %d, but got %d", expected, actual)
	}
}

func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
	input := long8nString
	expected := 56
	actual := GSM7Coder{}.EncodesInto(&input)
	if actual != expected {
		t.Errorf("Expected %d, but got %d", expected, actual)
	}
}

func TestGSM7DecodesIntoSmallString(t *testing.T) {
	input := short8nStringEncodedBytes
	expected := 8
	actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
	if actual != expected {
		t.Errorf("Expected %d, but got %d", expected, actual)
	}
}

func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
	input := longNot8nStringEncodedBytes
	expected := 37
	actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
	if actual != expected {
		t.Errorf("Expected %d, but got %d", expected, actual)
	}
}

func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
	input := long8nStringEncodedBytes
	expected := 64
	actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
	if actual != expected {
		t.Errorf("Expected %d, but got %d", expected, actual)
	}
}

// region benchmark
func BenchmarkGSM7EncodeSimpleASCIIString(b *testing.B) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := short8nString
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		coder.Encode(&input, &buf)
	}
}

func BenchmarkGSM7EncodeComplexASCIIString(b *testing.B) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := longNot8nString
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		coder.Encode(&input, &buf)
	}
}

func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) {
	coder := &GSM7Coder{}
	var buf bytes.Buffer
	input := long8nString
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		coder.Encode(&input, &buf)
	}
}

func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) {
	coder := &GSM7Coder{}
	input := long8nString
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
		buf.Reset()
		coder.Encode(&input, buf)
	}
}
