package pdu

import (
	"bytes"
	"testing"
)

// region encode
// See examples: https://www.openmarket.com/docs/Content/apis/v4smpp/mt-examples.htm
func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
	p := &SUBMIT_SM{
		header: &PDU_HEADER{
			command_length:  0,
			command_id:      4,
			command_status:  0,
			sequence_number: 378019,
		},
		service_type:           "OMV4",
		source_addr_ton:        3,
		source_addr_npi:        1,
		source_addr:            "80362",
		dest_addr_ton:          1,
		dest_addr_npi:          1,
		destination_addr:       "812345001000",
		esm_class:              0,
		protocol_id:            0,
		priority_flag:          0,
		schedule_delivery_time: "",
		validity_period:        "180105120000004+",
		registered_delivery:    1,
		data_coding:            1, // The example uses 0 and claims to use GSM but the message is encoded as ASCII...
		sm_default_msg_id:      0,
		short_message:          "Reply Yes to opt in or No to opt out.",
	}
	p.UpdateSize()
	buf := ByteBufferPool.Get(p.Size())
	err := p.Encode(buf)
	if err != nil {
		t.Fatalf("expected no error, got %v", err)
	}

	expected := []byte{0,0,0,107,0,0,0,4,0,0,0,0,0,5,196,163,79,77,86,52,0,3,1,56,48,51,54,50,0,1,1,56,49,50,51,52,53,48,48,49,48,48,48,0,0,0,0,0,49,56,48,49,48,53,49,50,48,48,48,48,48,48,52,43,0,1,0,0,0,37,82,101,112,108,121,32,89,101,115,32,116,111,32,111,112,116,32,105,110,32,111,114,32,78,111,32,116,111,32,111,112,116,32,111,117,116,46}
	if !bytes.Equal(buf.Bytes(), expected) {
		t.Fatalf("expected %v, got %v", expected, buf.Bytes())
	}
}

// func TestRealScenario(t *testing.T) {
//
// 	p.header.command_length = uint32(p.Size())
// 	p.sm_length = byte(len(p.short_message))
// 	buf := make([]byte, p.Size())
// 	err := p.EncodeInto(&buf)
// 	if err != nil {
// 		t.Errorf("Expected no error, got %v", err)
// 	}
// 	if len(buf) != len(expected) {
// 		t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf))
// 	}
// 	for i, v := range buf {
// 		if v != expected[i] {
// 			t.Errorf("Expected byte slice with values %v, got %v", expected, buf)
// 			break
// 		}
// 	}
// }

// region decode

// region size
func TestCalculateSizeTypicalInstance(t *testing.T) {
	p := &SUBMIT_SM{
		service_type:           "test_service",
		source_addr:            "12345",
		destination_addr:       "67890",
		schedule_delivery_time: "",
		validity_period:        "",
		short_message:          "Hello, World!",
	}
	expectedSize := 16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message)
	actualSize := p.Size()
	if actualSize != expectedSize {
		t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
	}
}

func TestCalculateSizeMaxLengths(t *testing.T) {
	maxLen := 255
	p := &SUBMIT_SM{
		service_type:           string(make([]byte, maxLen)),
		source_addr:            string(make([]byte, maxLen)),
		destination_addr:       string(make([]byte, maxLen)),
		schedule_delivery_time: string(make([]byte, maxLen)),
		validity_period:        string(make([]byte, maxLen)),
		short_message:          string(make([]byte, maxLen)),
	}
	expectedSize := 16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen
	actualSize := p.Size()
	if actualSize != expectedSize {
		t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
	}
}

func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
	p := &SUBMIT_SM{
		service_type:           "",
		source_addr:            "",
		destination_addr:       "",
		schedule_delivery_time: "",
		validity_period:        "",
		short_message:          "",
	}
	expectedSize := 16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message)
	actualSize := p.Size()
	if actualSize != expectedSize {
		t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
	}
}

// region benchmark
