#[macro_use] extern crate oatie;

use oatie::doc::*;
use oatie::Operation;
use oatie::{OT, debug_pretty};

fn main() {
    let op_left = op!(
        [
            DelWithGroup([
                DelWithGroup([]),
                DelSkip(2),
                DelGroup([DelSkip(6)])
            ])
        ],
        [
            AddWithGroup([
                AddWithGroup([]),
                AddSkip(2),
                AddChars("  5"),
                AddGroup({"client": "left", "tag": "caret"}, [])
            ])
        ]
    );
    let op_right = op!(
        [DelGroup([DelSkip(32)])],
        [AddGroup({"tag": "pre"}, [AddSkip(32)])]
    );
    
    let res = op_span!([DelGroup([DelWithGroup([]), DelSkip(2), DelGroup([]), DelSkip(25)])], [AddGroup({"tag": "pre"}, [AddWithGroup([]), AddSkip(2), AddChars("  5"), AddGroup({"client": "left", "tag": "caret"}, []), AddSkip(25)])]);
    
    let original = Doc(doc![
        DocGroup({"tag": "h2"}, [
            DocGroup({"tag": "caret", "client": "right"}, []),
            DocChars(" X"),
            DocGroup({"client": "left", "tag": "caret"}, [
                DocChars("a88MMd")
            ]),
            DocChars("1  N3vXHello world!")
        ]), DocGroup({"tag": "h2"}, [DocChars("D0G231m G8WC YJ 5hat\'s up with you?")])]);
    
    let latest_doc = Doc(doc![
        DocGroup({"tag": "h2"}, [
            DocGroup({"tag": "caret", "client": "right"}, []),
            DocChars(" X  5"),
            DocGroup({"client": "left", "tag": "caret"}, []),
            DocChars("a88MMd1  N3vXHello world!")
        ]),
        DocGroup({"tag": "h2"}, [DocChars("D0G231m G8WC YJ 5hat\'s up with you?")])]);

    // Works
    println!("1");
    let out = Op::apply(&latest_doc, &op_right);

    // Works
    println!("2");
    let out2 = Op::apply(&original, &op_left);
    println!("3 {}", debug_pretty(&out2));
    let out3 = Op::apply(&out2, &op_right);

    // Fails
    println!("4");
    let comp = Operation::compose(&op_left, &op_right);
    println!("OP {}", debug_pretty(&comp));
    let out4 = Op::apply(&original, &comp);

    // let mut ops = vec![
    //     op_span!(
    //         [DelWithGroup([DelSkip(10), DelGroup([])])],
    //         // [AddWithGroup([AddSkip(10), AddChars("Bh uh2"), AddGroup({"tag": "caret", "client": "left"}, [])])]
    //         [AddWithGroup([AddSkip(10), AddChars("123456"), AddGroup({"tag": "caret", "client": "left"}, [])])]
    //     ),
    //     op_span!(
    //         [DelGroup([DelSkip(73)])],
    //         [AddGroup({"tag": "h1"}, [AddSkip(73)])]
    //     ),
    // ];

    // let op = if ops.len() == 0 {
    //     (vec![], vec![])
    // } else {
    //     let mut op = ops.remove(0);
    //     for i in ops.into_iter() {
    //         op = Operation::compose(&op, &i);
    //     }
    //     op
    // };

    // println!("help me {:?}", op);
}
