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, 1, 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 TestEncodeFunctionCorrectlyEncodesAllFieldsGSM7Message(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:            0, // 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, 103, 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, 33, 210, 50, 156, 157, 7, 101, 203, 115, 16, 253, 13, 122, 195, 233, 160, 180, 27, 244, 150, 131, 156, 111, 16, 253, 13, 122, 195, 233, 160, 119, 157, 238, 2}
	if !bytes.Equal(buf.Bytes(), expected) {
		t.Fatalf("expected %v, got %v", expected, buf.Bytes())
	}
}

// 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!",
		Data_coding:            1,
	}
	expectedSize := 68
	actualSize := p.Size()
	if actualSize != expectedSize {
		t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
	}
}

func TestCalculateSizeTypicalGSM7Instance(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!",
		Data_coding:            0,
	}
	expectedSize := 67
	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)),
		Data_coding:            1,
	}
	expectedSize := 1563
	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 := 33
	actualSize := p.Size()
	if actualSize != expectedSize {
		t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
	}
}

// region benchmark
