1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::{
    collections::HashMap,
    hash::{DefaultHasher, Hash, Hasher},
    net::SocketAddr,
};

use bytes::{Buf, BufMut};

use crate::{
    common::{
        hasher::string_hasher::JavaStringHasher,
        message::{MessageConst, MessageTrait, MessageVersion},
        sys_flag::message_sys_flag::MessageSysFlag,
        TopicFilterType,
    },
    MessageUtils,
};

#[derive(Clone, Debug, Default)]
pub struct Message {
    pub topic: String,
    pub flag: i32,
    pub properties: HashMap<String, String>,
    pub body: Option<bytes::Bytes>,
    pub transaction_id: Option<String>,
}

impl Message {
    pub fn clear_property(&mut self, name: impl Into<String>) {
        self.properties.remove(name.into().as_str());
    }

    pub fn set_properties(&mut self, properties: HashMap<String, String>) {
        self.properties = properties;
    }

    pub fn get_property(&self, key: impl Into<String>) -> Option<String> {
        self.properties.get(key.into().as_str()).cloned()
    }

    pub fn body(&self) -> Option<bytes::Bytes> {
        self.body.as_ref().cloned()
    }

    pub fn flag(&self) -> i32 {
        self.flag
    }

    pub fn topic(&self) -> &str {
        &self.topic
    }
    pub fn properties(&self) -> &HashMap<String, String> {
        &self.properties
    }
    pub fn transaction_id(&self) -> Option<&str> {
        self.transaction_id.as_deref()
    }

    pub fn get_tags(&self) -> Option<String> {
        self.get_property(MessageConst::PROPERTY_TAGS)
    }

    pub fn is_wait_store_msg_ok(&self) -> bool {
        match self.get_property(MessageConst::PROPERTY_WAIT_STORE_MSG_OK) {
            None => true,
            Some(value) => value.parse().unwrap_or(true),
        }
    }
}

#[allow(unused_variables)]
impl MessageTrait for Message {
    fn topic(&self) -> &str {
        todo!()
    }

    fn with_topic(&mut self, topic: impl Into<String>) {
        todo!()
    }

    fn tags(&self) -> Option<&str> {
        todo!()
    }

    fn with_tags(&mut self, tags: impl Into<String>) {
        todo!()
    }

    fn put_property(&mut self, key: impl Into<String>, value: impl Into<String>) {
        todo!()
    }

    fn properties(&self) -> &HashMap<String, String> {
        todo!()
    }

    fn put_user_property(&mut self, name: impl Into<String>, value: impl Into<String>) {
        todo!()
    }

    fn delay_time_level(&self) -> i32 {
        todo!()
    }

    fn with_delay_time_level(&self, level: i32) -> i32 {
        todo!()
    }
}

#[derive(Clone, Debug)]
pub struct MessageExt {
    pub message: Message,
    pub broker_name: String,
    pub queue_id: i32,
    pub store_size: i32,
    pub queue_offset: i64,
    pub sys_flag: i32,
    pub born_timestamp: i64,
    pub born_host: SocketAddr,
    pub store_timestamp: i64,
    pub store_host: SocketAddr,
    pub msg_id: String,
    pub commit_log_offset: i64,
    pub body_crc: u32,
    pub reconsume_times: i32,
    pub prepared_transaction_offset: i64,
}

impl MessageExt {
    pub fn socket_address_2_byte_buffer(ip: &SocketAddr) -> bytes::Bytes {
        match ip {
            SocketAddr::V4(value) => {
                let mut byte_buffer = bytes::BytesMut::with_capacity(4 + 4);
                byte_buffer.put_slice(&value.ip().octets());
                byte_buffer.put_i32(value.port() as i32);
                byte_buffer.copy_to_bytes(byte_buffer.len())
            }
            SocketAddr::V6(value) => {
                let mut byte_buffer = bytes::BytesMut::with_capacity(16 + 4);
                byte_buffer.put_slice(&value.ip().octets());
                byte_buffer.put_i32(value.port() as i32);
                byte_buffer.copy_to_bytes(byte_buffer.len())
            }
        }
    }

    pub fn born_host_bytes(&self) -> bytes::Bytes {
        Self::socket_address_2_byte_buffer(&self.born_host)
    }

    pub fn born_store_bytes(&self) -> bytes::Bytes {
        Self::socket_address_2_byte_buffer(&self.store_host)
    }

    pub fn topic(&self) -> &str {
        self.message.topic()
    }

    pub fn born_host(&self) -> SocketAddr {
        self.born_host
    }

    pub fn store_host(&self) -> SocketAddr {
        self.store_host
    }

    pub fn with_born_host_v6_flag(&mut self) {
        self.sys_flag |= MessageSysFlag::BORNHOST_V6_FLAG;
    }

    pub fn with_store_host_v6_flag(&mut self) {
        self.sys_flag |= MessageSysFlag::STOREHOSTADDRESS_V6_FLAG;
    }

    pub fn body(&self) -> Option<bytes::Bytes> {
        self.message.body()
    }

    #[inline]
    pub fn sys_flag(&self) -> i32 {
        self.sys_flag
    }
    #[inline]
    pub fn body_crc(&self) -> u32 {
        self.body_crc
    }
    #[inline]
    pub fn queue_id(&self) -> i32 {
        self.queue_id
    }

    pub fn flag(&self) -> i32 {
        self.message.flag()
    }

    pub fn message_inner(&self) -> &Message {
        &self.message
    }
    pub fn broker_name(&self) -> &str {
        &self.broker_name
    }
    pub fn store_size(&self) -> i32 {
        self.store_size
    }
    pub fn queue_offset(&self) -> i64 {
        self.queue_offset
    }
    pub fn born_timestamp(&self) -> i64 {
        self.born_timestamp
    }
    pub fn store_timestamp(&self) -> i64 {
        self.store_timestamp
    }
    pub fn msg_id(&self) -> &str {
        &self.msg_id
    }
    pub fn commit_log_offset(&self) -> i64 {
        self.commit_log_offset
    }
    pub fn reconsume_times(&self) -> i32 {
        self.reconsume_times
    }
    pub fn prepared_transaction_offset(&self) -> i64 {
        self.prepared_transaction_offset
    }

    pub fn set_message_inner(&mut self, message_inner: Message) {
        self.message = message_inner;
    }
    pub fn set_broker_name(&mut self, broker_name: String) {
        self.broker_name = broker_name;
    }
    pub fn set_queue_id(&mut self, queue_id: i32) {
        self.queue_id = queue_id;
    }
    pub fn set_store_size(&mut self, store_size: i32) {
        self.store_size = store_size;
    }
    pub fn set_queue_offset(&mut self, queue_offset: i64) {
        self.queue_offset = queue_offset;
    }
    pub fn set_sys_flag(&mut self, sys_flag: i32) {
        self.sys_flag = sys_flag;
    }
    pub fn set_born_timestamp(&mut self, born_timestamp: i64) {
        self.born_timestamp = born_timestamp;
    }
    pub fn set_born_host(&mut self, born_host: SocketAddr) {
        self.born_host = born_host;
    }
    pub fn set_store_timestamp(&mut self, store_timestamp: i64) {
        self.store_timestamp = store_timestamp;
    }
    pub fn set_store_host(&mut self, store_host: SocketAddr) {
        self.store_host = store_host;
    }
    pub fn set_msg_id(&mut self, msg_id: String) {
        self.msg_id = msg_id;
    }
    pub fn set_commit_log_offset(&mut self, commit_log_offset: i64) {
        self.commit_log_offset = commit_log_offset;
    }
    pub fn set_body_crc(&mut self, body_crc: u32) {
        self.body_crc = body_crc;
    }
    pub fn set_reconsume_times(&mut self, reconsume_times: i32) {
        self.reconsume_times = reconsume_times;
    }
    pub fn set_prepared_transaction_offset(&mut self, prepared_transaction_offset: i64) {
        self.prepared_transaction_offset = prepared_transaction_offset;
    }

    pub fn properties(&self) -> &HashMap<String, String> {
        self.message.properties()
    }

    pub fn get_tags(&self) -> Option<String> {
        self.message.get_tags()
    }
}

impl Default for MessageExt {
    fn default() -> Self {
        Self {
            message: Default::default(),
            broker_name: "".to_string(),
            queue_id: 0,
            store_size: 0,
            queue_offset: 0,
            sys_flag: 0,
            born_timestamp: 0,
            born_host: "127.0.0.1:10911".parse().unwrap(),
            store_timestamp: 0,
            store_host: "127.0.0.1:10911".parse().unwrap(),
            msg_id: "".to_string(),
            commit_log_offset: 0,
            body_crc: 0,
            reconsume_times: 0,
            prepared_transaction_offset: 0,
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct MessageClientExt {
    pub message_ext_inner: MessageExt,
}

#[derive(Debug, Default)]
pub struct MessageExtBrokerInner {
    pub message_ext_inner: MessageExt,
    pub properties_string: String,
    pub tags_code: i64,
    pub encoded_buff: Option<bytes::BytesMut>,
    pub encode_completed: bool,
    pub version: MessageVersion,
}

impl MessageExtBrokerInner {
    const VERSION: MessageVersion = MessageVersion::V1;

    pub fn delete_property(&mut self, name: impl Into<String>) {
        let name = name.into();
        self.message_ext_inner.message.clear_property(name.as_str());
        self.properties_string =
            MessageUtils::delete_property(self.properties_string.as_str(), name.as_str());
    }

    pub fn with_version(&mut self, version: MessageVersion) {
        self.version = version;
    }

    pub fn version(&self) -> MessageVersion {
        self.version
    }

    pub fn topic(&self) -> &str {
        self.message_ext_inner.topic()
    }

    pub fn born_host(&self) -> SocketAddr {
        self.message_ext_inner.born_host()
    }

    pub fn store_host(&self) -> SocketAddr {
        self.message_ext_inner.store_host()
    }

    pub fn with_born_host_v6_flag(&mut self) {
        self.message_ext_inner.with_born_host_v6_flag()
    }

    pub fn with_store_host_v6_flag(&mut self) {
        self.message_ext_inner.with_store_host_v6_flag()
    }

    pub fn body(&self) -> Option<bytes::Bytes> {
        self.message_ext_inner.body()
    }

    pub fn sys_flag(&self) -> i32 {
        self.message_ext_inner.sys_flag()
    }
    pub fn body_crc(&self) -> u32 {
        self.message_ext_inner.body_crc()
    }
    pub fn queue_id(&self) -> i32 {
        self.message_ext_inner.queue_id()
    }
    pub fn flag(&self) -> i32 {
        self.message_ext_inner.flag()
    }
    pub fn born_timestamp(&self) -> i64 {
        self.message_ext_inner.born_timestamp()
    }

    pub fn store_timestamp(&self) -> i64 {
        self.message_ext_inner.store_timestamp()
    }

    pub fn born_host_bytes(&self) -> bytes::Bytes {
        self.message_ext_inner.born_host_bytes()
    }

    pub fn store_host_bytes(&self) -> bytes::Bytes {
        self.message_ext_inner.born_store_bytes()
    }

    pub fn reconsume_times(&self) -> i32 {
        self.message_ext_inner.reconsume_times()
    }
    pub fn prepared_transaction_offset(&self) -> i64 {
        self.message_ext_inner.prepared_transaction_offset()
    }

    pub fn property(&self, name: &str) -> Option<String> {
        self.message_ext_inner.properties().get(name).cloned()
    }

    pub fn properties_string(&self) -> &str {
        self.properties_string.as_str()
    }

    pub fn queue_offset(&self) -> i64 {
        self.message_ext_inner.queue_offset()
    }

    pub fn tags_string2tags_code(_filter: &TopicFilterType, tags: &str) -> i64 {
        if tags.is_empty() {
            return 0;
        }
        JavaStringHasher::new().hash_str(tags) as i64
    }

    pub fn get_tags(&self) -> Option<String> {
        self.message_ext_inner.get_tags()
    }

    pub fn is_wait_store_msg_ok(&self) -> bool {
        self.message_ext_inner.message.is_wait_store_msg_ok()
    }

    pub fn body_len(&self) -> usize {
        self.message_ext_inner.message.body.as_ref().unwrap().len()
    }
}

pub fn parse_topic_filter_type(sys_flag: i32) -> TopicFilterType {
    if (sys_flag & MessageSysFlag::MULTI_TAGS_FLAG) == MessageSysFlag::MULTI_TAGS_FLAG {
        TopicFilterType::MultiTag
    } else {
        TopicFilterType::SingleTag
    }
}

pub fn tags_string2tags_code(tags: Option<&String>) -> i64 {
    if tags.is_none() {
        return 0;
    }
    let tags = tags.unwrap();
    if tags.is_empty() {
        return 0;
    }
    JavaStringHasher::new().hash_str(tags.as_str()) as i64
}