//! # SabiWM - 錆WM (Rust WM)
//!
//! SabiWM is a window manager written entirely in safe Rust.
//! It aims to be a reliable, highly customizable tiling window manager
//! in the spirit of XMonad, I3, bspwm and the like.
//!
//! ## SabiWM Lib
//!
//! The core part of SabiWM is its library. It serves as a
//! learning basis for people who want to write their own window manager
//! or as an easy place to customize it.
//!
//! The library itself is split into several parts, shortly explained below:
//!
//! ### Core
//!
//! The [`Core`] module
//!
//! The core module contains all the necessary data structures to handle the internal
//! state to tile windows, manage focus, screens, workspaces, etc.
//!
//! ### Backend
//!
//! The backend module contains the general backend trait to abstract
//! away from all the different backends, e.g. XCB, Wayland, Redox and all the others out there.
//!
//! ### Config
//!
//! The config module will contain several ways to configure the WM. All of them shall
//! be interchangeable. The first and easiest way is a simple TOML file and on top of that
//! a ctl daemon, a LUA interface and direct IPC.
//!
//! [`Core`]: core/index.html
//! ['Backend']: backend/index.html

#![deny(missing_docs)]

extern crate chrono;
#[macro_use]
extern crate error_chain;
extern crate fern;
#[macro_use]
extern crate log;
extern crate xcb;
extern crate xdg;

#[macro_use]
mod macros;
pub mod backend;
pub mod core;

mod errors {
    error_chain!{
        foreign_links {
            Io(::std::io::Error);
            Log(::log::SetLoggerError);
        }
    }
}

use errors::*;
use backend::{Backend, Event};
use xdg::BaseDirectories;

/// Run the actual window manager
pub fn run() -> Result<()> {
    initialize_logger()
        .chain_err(|| "unable to initialize logger")?;

    let xcb = backend::Xcb::new()?;
    let mut workspace: core::Workspace<u32> = core::Workspace::new(0, "Main", None);

    loop {
        match xcb.event() {
            Event::WindowCreated(window) => {
                if !xcb.is_window(window) {
                    continue;
                }
                xcb.resize_window(window, 50, 50);
                workspace = workspace.add(window);
            }
            Event::WindowClosed(window) => {

           _ => (),
        }
    }
}

/// Initialize the logger
pub fn initialize_logger() -> Result<()> {
    let xdg = BaseDirectories::with_prefix("sabiwm")
        .chain_err(|| "unable to get xdg base directory")?;
    let path = xdg.place_cache_file("sabiwm.log")
        .chain_err(|| "unable to get path for log file")?;

    fern::Dispatch::new()
        .format(|out, message, record| {
                    out.finish(format_args!("{}[{}][{}] {}",
                                            chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
                                            record.target(),
                                            record.level(),
                                            message))
                })
        .level(log::LogLevelFilter::Debug)
        .chain(std::io::stdout())
        .chain(fern::log_file(path)?)
        .apply()?;

    info!("initialized logger");
    info!("sabiwm version {}", env!("CARGO_PKG_VERSION"));

    Ok(())
}

                workspace = workspace.remove(window);
            }
 