use std::vec;

use crate::*;

#[test]
fn uncompressed() {
    // Simple example generated by gpac
    const ENCODED: &[u8] = include_bytes!("uncompressed.mp4");

    let buf = &mut std::io::Cursor::new(ENCODED);
    let ftyp = Ftyp::decode(buf).expect("failed to decode ftyp");

    assert_eq!(
        ftyp,
        Ftyp {
            major_brand: b"isom".into(),
            minor_version: 1,
            compatible_brands: vec![b"isom".into(),],
        }
    );

    let moov = Moov::decode(buf).expect("failed to decode moov");

    assert_eq!(
        moov,
        Moov {
            mvhd: Mvhd {
                creation_time: 3827291266,
                modification_time: 3827291266,
                timescale: 600,
                duration: 1200,
                rate: 1.into(),
                volume: 1.into(),
                matrix: Matrix {
                    a: 65536,
                    b: 0,
                    u: 0,
                    c: 0,
                    d: 65536,
                    v: 0,
                    x: 0,
                    y: 0,
                    w: 1073741824
                },
                next_track_id: 2
            },
            trak: [Trak {
                tkhd: Tkhd {
                    creation_time: 3827291266,
                    modification_time: 3827291266,
                    track_id: 1,
                    duration: 1200,
                    layer: 0,
                    alternate_group: 0,
                    enabled: true,
                    in_movie: true,
                    size_is_aspect_ratio: false,
                    volume: 0.into(),
                    matrix: Matrix {
                        a: 65536,
                        b: 0,
                        u: 0,
                        c: 0,
                        d: 65536,
                        v: 0,
                        x: 0,
                        y: 0,
                        w: 1073741824
                    },
                    width: 2.into(),
                    height: 1.into(),
                },
                mdia: Mdia {
                    mdhd: Mdhd {
                        creation_time: 3827291266,
                        modification_time: 3827291266,
                        timescale: 1,
                        duration: 2,
                        language: "und".to_string(),
                    },
                    hdlr: Hdlr {
                        handler: FourCC::new(b"vide"),
                        name: "GPAC ISO Video Handler".into(),
                    },
                    minf: Minf {
                        vmhd: Some(Vmhd {
                            graphics_mode: 0,
                            op_color: RgbColor {
                                red: 0,
                                green: 0,
                                blue: 0
                            }
                        }),
                        dinf: Dinf {
                            dref: Dref {
                                urls: vec![Url {
                                    location: "".into()
                                }],
                            }
                        },
                        stbl: Stbl {
                            stsd: Stsd {
                                codecs: vec![Codec::Uncv(Uncv {
                                    visual: Visual {
                                        data_reference_index: 1,
                                        width: 2u16,
                                        height: 1u16,
                                        horizresolution: FixedPoint::new(0, 72),
                                        vertresolution: FixedPoint::new(0, 72),
                                        frame_count: 1,
                                        compressor: "Raw Video".into(),
                                        depth: 24
                                    },
                                    cmpd: Some(Cmpd {
                                        components: vec![
                                            Component {
                                                component_type: 4,
                                                component_type_uri: None
                                            },
                                            Component {
                                                component_type: 5,
                                                component_type_uri: None
                                            },
                                            Component {
                                                component_type: 6,
                                                component_type_uri: None
                                            }
                                        ]
                                    }),
                                    uncc: UncC::V0 {
                                        profile: FourCC::new(b"\0\0\0\0"),
                                        components: vec![
                                            UncompressedComponent {
                                                component_index: 0,
                                                component_bit_depth_minus_one: 7,
                                                component_format: 0,
                                                component_align_size: 0
                                            },
                                            UncompressedComponent {
                                                component_index: 1,
                                                component_bit_depth_minus_one: 7,
                                                component_format: 0,
                                                component_align_size: 0
                                            },
                                            UncompressedComponent {
                                                component_index: 2,
                                                component_bit_depth_minus_one: 7,
                                                component_format: 0,
                                                component_align_size: 0
                                            },
                                        ],
                                        sampling_type: 0,
                                        interleave_type: 1,
                                        block_size: 0,
                                        components_little_endian: false,
                                        block_pad_lsb: false,
                                        block_little_endian: false,
                                        block_reversed: false,
                                        pad_unknown: false,
                                        pixel_size: 0,
                                        row_align_size: 0,
                                        tile_align_size: 0,
                                        num_tile_cols_minus_one: 0,
                                        num_tile_rows_minus_one: 0
                                    },
                                    btrt: Some(Btrt {
                                        buffer_size_db: 6,
                                        max_bitrate: 48,
                                        avg_bitrate: 48
                                    }),
                                    ccst: None,
                                    pasp: None,
                                    taic: None,
                                })],
                            },
                            stts: Stts {
                                entries: [SttsEntry {
                                    sample_count: 2,
                                    sample_delta: 1
                                }]
                                .into()
                            },
                            ctts: None,
                            stss: None,
                            stsc: Stsc {
                                entries: [
                                    StscEntry {
                                        first_chunk: 1,
                                        samples_per_chunk: 1,
                                        sample_description_index: 1
                                    },
                                    StscEntry {
                                        first_chunk: 2,
                                        samples_per_chunk: 1,
                                        sample_description_index: 1
                                    }
                                ]
                                .into()
                            },
                            stsz: Stsz {
                                samples: StszSamples::Identical { count: 2, size: 6 },
                            },
                            stco: Some(Stco {
                                entries: [856, 862].into()
                            }),
                            co64: None,
                            sbgp: vec![],
                            sgpd: vec![],
                            subs: vec![],
                            saiz: vec![],
                            saio: vec![],
                            cslg: None,
                        },
                        ..Default::default()
                    }
                },
                ..Default::default()
            }]
            .into(),
            udta: Some(Udta {
                meta: Some(Meta {
                    hdlr: Hdlr {
                        handler: FourCC::new(b"mdir"),
                        name: "".into()
                    },
                    items: vec![Ilst {
                        name: None,
                        year: None,
                        covr: None,
                        desc: None,
                        ctoo: Some(Tool {
                            country_indicator: 0,
                            language_indicator: 0,
                            text: "GPAC-2.5-DEV-rev2076-gd245ba575-rawff_amd2_2024-07-06".into()
                        })
                    }
                    .into(),],
                }),
                ..Default::default()
            },),
            ..Default::default()
        }
    );

    let mdat = Mdat::decode(buf).expect("failed to decode mdat");

    let mut buf = Vec::new();
    ftyp.encode(&mut buf).expect("failed to encode ftyp");
    moov.encode(&mut buf).expect("failed to encode moov");
    mdat.encode(&mut buf).expect("failed to encode mdat");

    // assert_eq!(buf, ENCODED);
}
