//
// Copyright (C) 2024 Automated Design Corp.. All Rights Reserved.
// Created Date: 2024-11-12 07:48:14
// -----
// Last Modified: 2024-11-12 09:33:02
// -----
//
//

use crc32fast::Hasher;
use mechutil::{
    command::{MechFsmCommandTuple, MechFsmControl, MechFsmState, MechFsmStatusTuple},
    command_arg_tuple::MechFsmCommandArgTuple,
};

fn main() {
    let mut arg = MechFsmCommandArgTuple::new();
    let _ = arg.push_value_f32(-293.7);
    let _ = arg.push_value_f32(-9.9);
    let _ = arg.push_value_f32(500.3);
    let _ = arg.push_value_f32(180.0);
    let _ = arg.push_value_f32(0.0);
    let _ = arg.push_value_f32(0.0);
    let _ = arg.push_value_u16(1234);
    let _ = arg.push_value_bool(true);

    println!("arg: {:?}", arg);

    let bytes = arg.to_bytes();

    println!("BYTES: {:?}", bytes);

    for i in 510..527 {
        println!("bytes[{}] = {}", i, bytes[i]);
    }

    for i in 765..775 {
        println!("bytes[{}] = {}", i, bytes[i]);
    }

    println!("bytes[1020] = {}", bytes[1020]);
    println!("bytes[1021] = {}", bytes[1021]);
    // println!("bytes[1022] = {}", bytes[1022]);

    // Serialize the struct without the crc field
    let mut hasher = Hasher::new();

    hasher.update(&bytes);
    let crc32 = hasher.finalize();

    println!("ARG CRC32 = {}", crc32);

    let mut cmd_hash_test = MechFsmCommandTuple::new();
    cmd_hash_test.transaction_id = 2000;
    cmd_hash_test.control = MechFsmControl::Idle;
    cmd_hash_test.index = 7;
    cmd_hash_test.sub_index = 9;
    cmd_hash_test.data = arg.clone();
    cmd_hash_test.update_crc32();

    println!("CMD CRC32 = {}", cmd_hash_test.crc);

    let mut status_hash_test = MechFsmStatusTuple::new();
    status_hash_test.state = MechFsmState::Init;
    status_hash_test.transaction_id = 12345;
    status_hash_test.data = arg.clone();
    status_hash_test.update_crc32();

    println!("STATUS CRC32 = {}", status_hash_test.crc);

    // assert_eq!(status_hash_test.crc, 398265339);

    // assert_eq!(cmd_hash_test.crc, 2109466950);

    let json = serde_json::to_value(&status_hash_test).unwrap();
    println!("STATUS JSON\n{}", json);
}
