#![deny(rust_2018_idioms, unsafe_code)]
#![warn(
    absolute_paths_not_starting_with_crate,
    ambiguous_associated_items,
    anonymous_parameters,
    arithmetic_overflow,
    array_into_iter,
    asm_sub_register,
    bad_asm_style,
    bindings_with_variant_name,
    break_with_label_and_loop,
    clashing_extern_declarations,
    coherence_leak_check,
    conflicting_repr_hints,
    confusable_idents,
    const_evaluatable_unchecked,
    const_item_mutation,
    dangling_pointers_from_temporaries,
    dead_code,
    deprecated_in_future,
    deprecated_where_clause_location,
    deprecated,
    deref_into_dyn_supertrait,
    deref_nullptr,
    drop_bounds,
    duplicate_macro_attributes,
    dyn_drop,
    ellipsis_inclusive_range_patterns,
    enum_intrinsics_non_enums,
    explicit_outlives_requirements,
    exported_private_dependencies,
    forbidden_lint_groups,
    function_item_references,
    future_incompatible,
    ill_formed_attribute_input,
    improper_ctypes_definitions,
    improper_ctypes,
    incomplete_features,
    incomplete_include,
    ineffective_unstable_trait_impl,
    inline_no_sanitize,
    invalid_atomic_ordering,
    invalid_doc_attributes,
    invalid_type_param_default,
    invalid_value,
    irrefutable_let_patterns,
    keyword_idents,
    large_assignments,
    late_bound_lifetime_arguments,
    legacy_derive_helpers,
    macro_expanded_macro_exports_accessed_by_absolute_paths,
    meta_variable_misuse,
    missing_abi,
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs,
    mixed_script_confusables,
    mutable_transmutes,
    named_arguments_used_positionally,
    named_asm_labels,
    no_mangle_const_items,
    no_mangle_generic_items,
    non_ascii_idents,
    non_camel_case_types,
    non_fmt_panics,
    non_shorthand_field_patterns,
    non_snake_case,
    non_upper_case_globals,
    nonstandard_style,
    noop_method_call,
    overflowing_literals,
    overlapping_range_endpoints,
    path_statements,
    patterns_in_fns_without_body,
    proc_macro_derive_resolution_fallback,
    pub_use_of_private_extern_crate,
    redundant_semicolons,
    repr_transparent_external_private_fields,
    rust_2021_incompatible_closure_captures,
    rust_2021_incompatible_or_patterns,
    rust_2021_prefixes_incompatible_syntax,
    rust_2021_prelude_collisions,
    semicolon_in_expressions_from_macros,
    soft_unstable,
    stable_features,
    text_direction_codepoint_in_comment,
    text_direction_codepoint_in_literal,
    trivial_bounds,
    trivial_casts,
    trivial_numeric_casts,
    type_alias_bounds,
    tyvar_behind_raw_pointer,
    uncommon_codepoints,
    unconditional_panic,
    unconditional_recursion,
    unexpected_cfgs,
    uninhabited_static,
    unknown_crate_types,
    unnameable_test_items,
    unreachable_code,
    unreachable_patterns,
    unreachable_pub,
    unsafe_op_in_unsafe_fn,
    unstable_features,
    unstable_name_collisions,
    unused_allocation,
    unused_assignments,
    unused_attributes,
    unused_braces,
    unused_comparisons,
    unused_crate_dependencies,
    unused_doc_comments,
    unused_extern_crates,
    unused_features,
    unused_import_braces,
    unused_imports,
    unused_labels,
    unused_lifetimes,
    unused_macro_rules,
    unused_macros,
    unused_must_use,
    unused_mut,
    unused_parens,
    unused_qualifications,
    unused_unsafe,
    unused_variables,
    useless_deprecated,
    while_true
)]
#![warn(
    clippy::all,
    clippy::await_holding_lock,
    clippy::char_lit_as_u8,
    clippy::checked_conversions,
    clippy::cognitive_complexity,
    clippy::dbg_macro,
    clippy::debug_assert_with_mut_call,
    clippy::disallowed_script_idents,
    clippy::doc_link_with_quotes,
    clippy::doc_markdown,
    clippy::empty_enum,
    clippy::empty_line_after_outer_attr,
    clippy::empty_structs_with_brackets,
    clippy::enum_glob_use,
    clippy::equatable_if_let,
    clippy::exit,
    clippy::expl_impl_clone_on_copy,
    clippy::explicit_deref_methods,
    clippy::explicit_into_iter_loop,
    clippy::fallible_impl_from,
    clippy::filter_map_next,
    clippy::flat_map_option,
    clippy::float_cmp_const,
    clippy::float_cmp,
    clippy::float_equality_without_abs,
    clippy::fn_params_excessive_bools,
    clippy::fn_to_numeric_cast_any,
    clippy::from_iter_instead_of_collect,
    clippy::if_let_mutex,
    clippy::implicit_clone,
    clippy::imprecise_flops,
    clippy::index_refutable_slice,
    clippy::inefficient_to_string,
    clippy::invalid_upcast_comparisons,
    clippy::iter_not_returning_iterator,
    clippy::large_digit_groups,
    clippy::large_stack_arrays,
    clippy::large_types_passed_by_value,
    clippy::let_unit_value,
    clippy::linkedlist,
    clippy::lossy_float_literal,
    clippy::macro_use_imports,
    clippy::manual_ok_or,
    clippy::map_err_ignore,
    clippy::map_flatten,
    clippy::map_unwrap_or,
    clippy::match_same_arms,
    clippy::match_wild_err_arm,
    clippy::match_wildcard_for_single_variants,
    clippy::mem_forget,
    clippy::missing_const_for_fn,
    clippy::missing_enforced_import_renames,
    clippy::missing_errors_doc,
    clippy::missing_panics_doc,
    clippy::mut_mut,
    clippy::mutex_integer,
    clippy::needless_borrow,
    clippy::needless_continue,
    clippy::needless_for_each,
    clippy::needless_pass_by_value,
    clippy::negative_feature_names,
    clippy::nonstandard_macro_braces,
    clippy::nursery,
    clippy::option_if_let_else,
    clippy::option_option,
    clippy::path_buf_push_overwrite,
    clippy::pedantic,
    clippy::print_stderr,
    clippy::print_stdout,
    clippy::ptr_as_ptr,
    clippy::rc_mutex,
    clippy::ref_option_ref,
    clippy::rest_pat_in_fully_bound_structs,
    clippy::same_functions_in_if_condition,
    clippy::semicolon_if_nothing_returned,
    clippy::shadow_unrelated,
    clippy::similar_names,
    clippy::single_match_else,
    clippy::string_add_assign,
    clippy::string_add,
    clippy::string_lit_as_bytes,
    clippy::suspicious_operation_groupings,
    clippy::todo,
    clippy::trailing_empty_array,
    clippy::trait_duplication_in_bounds,
    clippy::trivially_copy_pass_by_ref,
    clippy::unimplemented,
    clippy::unnecessary_wraps,
    clippy::unnested_or_patterns,
    clippy::unseparated_literal_suffix,
    clippy::unused_self,
    clippy::use_debug,
    clippy::use_self,
    clippy::used_underscore_binding,
    clippy::useless_let_if_seq,
    clippy::useless_transmute,
    clippy::verbose_file_reads,
    clippy::wildcard_dependencies,
    clippy::wildcard_imports,
    clippy::zero_sized_map_values
)]

//! # sable-canvas
//!
//! `sable-canvas` is a library for creating and manipulating JSON objects representing a canvas.
//!
//! Specification source: <https://jsoncanvas.org/>
//!
//! ## Example
//!
//! ```
//! use sable_canvas::Canvas;
//! let s: String = "{\"nodes\":[{\"id\":\"id7\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"background\":\"path/to/image.png\",\"type\":\"group\"},{\"id\":\"id5\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"color\":\"#ff0000\",\"label\":\"Label\",\"type\":\"group\"},{\"id\":\"id2\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"color\":\"1\",\"file\":\"dir/to/path/file.png\",\"type\":\"file\"},{\"id\":\"id4\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"color\":\"1\",\"url\":\"https://www.google.com\",\"type\":\"link\"},{\"id\":\"id6\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"type\":\"group\"},{\"id\":\"id3\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"color\":\"1\",\"file\":\"dir/to/path/file.png\",\"subpath\":\"#here\",\"type\":\"file\"},{\"id\":\"id8\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"background\":\"path/to/image.png\",\"backgroundStyle\":\"cover\",\"type\":\"group\"},{\"id\":\"id\",\"x\":0,\"y\":0,\"width\":100,\"height\":100,\"color\":\"1\",\"text\":\"Test\",\"type\":\"text\"}],\"edges\":[{\"id\":\"edge2\",\"fromNode\":\"node3\",\"toNode\":\"node4\",\"color\":\"5\",\"label\":\"edge label\",\"toSide\":\"left\",\"toEnd\":\"arrow\"},{\"id\":\"edge1\",\"fromNode\":\"node1\",\"toNode\":\"node2\",\"toSide\":\"left\",\"toEnd\":\"arrow\"}]}".to_string();
//! let canvas: Canvas = s.parse().unwrap();
//!
//! let _s = canvas.to_string();
//! ```
//!
//! ## Complete example
//!
//! ```rust
//! use std::path::PathBuf;
//!
//! use sable_canvas::{
//!     Canvas,
//!     Color, HexColor, PresetColor,
//!     Edge, End, Side,
//!     Background, BackgroundStyle, FileNode, GroupNode, LinkNode, Node, TextNode,
//! };
//! use url::Url;
//!
//! // Color
//! let color1 = Color::Preset(PresetColor::Red);
//! let color2 = Color::Color(HexColor::parse("#ff0000").unwrap());
//!
//! let serialized_color1 = serde_json::to_string(&color1).unwrap();
//! let serialized_color2 = serde_json::to_string(&color2).unwrap();
//!
//! println!("serialized1 = {}", serialized_color1);
//! println!("serialized2 = {}", serialized_color2);
//!
//! // Text Node
//! let node1: Node = Node::Text(TextNode::new(
//!     "id".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     Some(Color::Preset(PresetColor::Red)),
//!     "This is a test".to_string(),
//! ));
//!
//! // File Node
//! let node2: Node = Node::File(FileNode::new(
//!     "id2".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     Some(Color::Preset(PresetColor::Red)),
//!     PathBuf::from("dir/to/path/file.png"),
//!     None,
//! ));
//! let node3: Node = Node::File(FileNode::new(
//!     "id3".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     Some(color1),
//!     PathBuf::from("dir/to/path/file.png"),
//!     Some("#here".parse().unwrap()),
//! ));
//!
//! // Link Node
//! let node4: Node = Node::Link(LinkNode::new(
//!     "id4".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     Some(Color::Preset(PresetColor::Red)),
//!     Url::parse("https://julienduroure.com").unwrap(),
//! ));
//!
//! // Group Node
//! let node5: Node = Node::Group(GroupNode::new(
//!     "id5".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     Some(color2),
//!     Some("Label".to_string()),
//!     None,
//! ));
//! let node6: Node = Node::Group(GroupNode::new(
//!     "id6".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     None,
//!     None,
//!     None,
//! ));
//! let node7: Node = Node::Group(GroupNode::new(
//!     "id7".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     None,
//!     None,
//!     Some(Background::new(PathBuf::from("path/to/image.png"), None)),
//! ));
//! let node8: Node = Node::Group(GroupNode::new(
//!     "id8".parse().unwrap(),
//!     0,
//!     0,
//!     100,
//!     100,
//!     None,
//!     None,
//!     Some(Background::new(
//!         PathBuf::from("path/to/image.png"),
//!         Some(BackgroundStyle::Cover),
//!     )),
//! ));
//!
//! let serialized_node1: String = serde_json::to_string(&node1).unwrap();
//! let serialized_node2 = serde_json::to_string(&node2).unwrap();
//! let serialized_node3 = serde_json::to_string(&node3).unwrap();
//! let serialized_node4 = serde_json::to_string(&node4).unwrap();
//! let serialized_node5 = serde_json::to_string(&node5).unwrap();
//! let serialized_node6 = serde_json::to_string(&node6).unwrap();
//! let serialized_node7 = serde_json::to_string(&node7).unwrap();
//! let serialized_node8 = serde_json::to_string(&node8).unwrap();
//!
//! println!("serialized node 1= {}", serialized_node1);
//! println!("serialized node 2= {}", serialized_node2);
//! println!("serialized node 3= {}", serialized_node3);
//! println!("serialized node 4= {}", serialized_node4);
//! println!("serialized node 5= {}", serialized_node5);
//! println!("serialized node 6= {}", serialized_node6);
//! println!("serialized node 7= {}", serialized_node7);
//! println!("serialized node 8= {}", serialized_node8);
//!
//! // Edge
//! let edge1 = Edge::new(
//!     "edge1".parse().unwrap(),
//!     "id".parse().unwrap(),
//!     None,
//!     None,
//!     "id2".parse().unwrap(),
//!     Some(Side::Left),
//!     Some(End::Arrow),
//!     None,
//!     None,
//! );
//! let edge2 = Edge::new(
//!     "edge2".parse().unwrap(),
//!     "id3".parse().unwrap(),
//!     None,
//!     None,
//!     "id4".parse().unwrap(),
//!     Some(Side::Left),
//!     Some(End::Arrow),
//!     Some(Color::Preset(PresetColor::Cyan)),
//!     Some("edge label".to_string()),
//! );
//!
//! let serialized_edge1 = serde_json::to_string(&edge1).unwrap();
//! let serialized_edge2 = serde_json::to_string(&edge2).unwrap();
//!
//! println!("serialized edge 1= {}", serialized_edge1);
//! println!("serialized edge 2= {}", serialized_edge2);
//!
//! // JSON Canvas
//! let mut canvas = Canvas::default();
//!
//! let empty_canvas = canvas.to_string();
//! println!("empty canvas = {}", empty_canvas);
//! canvas = empty_canvas.parse().unwrap();
//!
//! canvas.add_node(node1).unwrap();
//! canvas.add_node(node2).unwrap();
//! canvas.add_node(node3).unwrap();
//! canvas.add_node(node4).unwrap();
//! canvas.add_node(node5).unwrap();
//! canvas.add_node(node6).unwrap();
//! canvas.add_node(node7).unwrap();
//! canvas.add_node(node8).unwrap();
//!
//! canvas.add_edge(edge1).unwrap();
//! canvas.add_edge(edge2).unwrap();
//!
//! let serialized_canvas = canvas.to_string();
//!
//! println!("serialized canvas = {}", serialized_canvas);
//!
//! let jsoncanvas_deserialized: Canvas = serialized_canvas.parse().unwrap();
//! println!("deserialized canvas = {:?}", jsoncanvas_deserialized);
//! ```
//!
//! ## Available structs
//!
//! ```
//! use sable_canvas::{
//!     Canvas,
//!     Color, HexColor, PresetColor,
//!     Edge, End, Side,
//!     Background, BackgroundStyle, FileNode, GroupNode, LinkNode, Node, TextNode,
//! };
//! ```

mod canvas;
mod color;
mod edge;
mod id;
mod node;

pub use crate::{
    canvas::{Canvas, CanvasError},
    color::{Color, HexColor, PresetColor},
    edge::{Edge, End, Side, Terminus},
    id::{EdgeId, EmptyId, NodeId},
    node::{
        Background, BackgroundStyle, FileNode, GenericNode, GenericNodeInfo, GroupNode, LinkNode,
        Node, TextNode,
    },
};

/// Type alias for the pixel coordinate unit.
pub type PixelCoordinate = i64;
/// Type alias for the pixel dimension unit.
pub type PixelDimension = u64;

#[cfg(test)]
mod test {
    use hex_color::HexColor;

    #[allow(clippy::too_many_lines, reason = "this is a test... :/")]
    #[allow(clippy::print_stdout)]
    #[test]
    fn test() {
        use std::path::PathBuf;

        use url::Url;

        use super::{
            canvas::Canvas,
            color::{Color, PresetColor},
            edge::{Edge, End, Side},
            node::{Background, BackgroundStyle, FileNode, GroupNode, LinkNode, Node, TextNode},
        };

        // Color
        let color1 = Color::Preset(PresetColor::Red);
        let color2 = Color::Color(HexColor::parse("#ff0000").unwrap());

        // Text Node
        let node1: Node = TextNode::new(
            "id".parse().unwrap(),
            0,
            0,
            100,
            100,
            Some(Color::Preset(PresetColor::Red)),
            "This is a test".to_string(),
        )
        .into();

        // File Node
        let node2: Node = FileNode::new(
            "id2".parse().unwrap(),
            0,
            0,
            100,
            100,
            Some(Color::Preset(PresetColor::Red)),
            PathBuf::from("dir/to/path/file.png"),
            None,
        )
        .into();
        let node3: Node = FileNode::new(
            "id3".parse().unwrap(),
            0,
            0,
            100,
            100,
            Some(color1),
            PathBuf::from("dir/to/path/file.png"),
            Some("#here".to_string()),
        )
        .into();

        // Link Node
        let node4: Node = LinkNode::new(
            "id4".parse().unwrap(),
            0,
            0,
            100,
            100,
            Some(Color::Preset(PresetColor::Red)),
            Url::parse("https://julienduroure.com").unwrap(),
        )
        .into();

        // Group Node
        let node5: Node = GroupNode::new(
            "id5".parse().unwrap(),
            0,
            0,
            100,
            100,
            Some(color2),
            Some("Label".to_string()),
            None,
        )
        .into();
        let node6: Node =
            GroupNode::new("id6".parse().unwrap(), 0, 0, 100, 100, None, None, None).into();
        let node7: Node = GroupNode::new(
            "id7".parse().unwrap(),
            0,
            0,
            100,
            100,
            None,
            None,
            Some(Background::new(PathBuf::from("path/to/image.png"), None)),
        )
        .into();
        let node8: Node = GroupNode::new(
            "id8".parse().unwrap(),
            0,
            0,
            100,
            100,
            None,
            None,
            Some(Background::new(
                PathBuf::from("path/to/image.png"),
                Some(BackgroundStyle::Cover),
            )),
        )
        .into();

        // Edge

        let edge1 = Edge::new(
            "edge1".parse().unwrap(),
            "id".parse().unwrap(),
            None,
            None,
            "id2".parse().unwrap(),
            Some(Side::Left),
            Some(End::Arrow),
            None,
            None,
        );
        let edge2 = Edge::new(
            "edge2".parse().unwrap(),
            "id3".parse().unwrap(),
            None,
            None,
            "id4".parse().unwrap(),
            Some(Side::Left),
            Some(End::Arrow),
            Some(Color::Preset(PresetColor::Cyan)),
            Some("edge label".to_string()),
        );

        // JSON Canvas
        let mut canvas = Canvas::default();
        canvas.add_node(node1).unwrap();
        canvas.add_node(node2).unwrap();
        canvas.add_node(node3).unwrap();
        canvas.add_node(node4).unwrap();
        canvas.add_node(node5).unwrap();
        canvas.add_node(node6).unwrap();
        canvas.add_node(node7).unwrap();
        canvas.add_node(node8).unwrap();

        canvas.add_edge(edge1).unwrap();
        canvas.add_edge(edge2).unwrap();

        let serialized_canvas = canvas.to_string();

        println!("serialized canvas = {serialized_canvas}");

        ///////////////////////////// Deserialization /////////////////////////////

        // let deserialized_node1: Node = serde_json::from_str(&serialized_node1).unwrap();
        // println!("deserialized node 1= {:?}", deserialized_node1);

        // let deseralied_edge1: Edge = serde_json::from_str(&serialized_edge1).unwrap();
        // println!("deserialized edge 1= {:?}", deseralied_edge1);

        let _jsoncanvas_deserialized: Canvas = serialized_canvas.parse().unwrap();
    }
}
