A KVM switch emulator using UDP/IP
use crate::cluster::{Cluster, Screen, Focus};

use futures::{Sink, Stream};
use serde::{Serialize, Deserialize};

use std::io;

pub trait HostInterface
    : Stream<Item=io::Result<HostOutputEvent>>
    + Sink<HostInputEvent, Error=io::Error>
{
    fn screen_size(&self) -> (i32, i32);
    fn cursor_pos(&self) -> (i32, i32);
}

pub trait NetworkInterface<Addr>
    : Stream<Item=bincode::Result<(NetEvent, Addr)>>
    + Sink<(NetEvent, Addr), Error=bincode::Error>
    + Sink<NetEvent, Error=bincode::Error>
{
}

#[derive(Serialize, Deserialize, Debug)]
pub enum InputEvent {
    Host(HostInputEvent),
    Net(NetEvent),
}

#[derive(Serialize, Deserialize, Debug)]
pub enum HostEvent {
    Input(HostInputEvent),
    Output(HostOutputEvent),
}

#[derive(Serialize, Deserialize, Debug)]
pub enum HostInputEvent {
    Position(PositionEvent),
    Grab(GrabEvent),
    Button(ButtonEvent),
    Key(KeyEvent),
    Selection(SelectionEvent),
}

#[derive(Serialize, Deserialize, Debug)]
pub enum HostOutputEvent {
    Motion(MotionEvent),
    Button(ButtonEvent),
    Key(KeyEvent),
    Selection(SelectionEvent),
}

#[derive(Serialize, Deserialize, Debug)]
pub enum NetEvent {
    Connect(Cluster),
    RequestCluster,
    Cluster(Cluster),
    Screens(Vec<Screen>),
    Focus(Focus),
    Button(ButtonEvent),
    Key(KeyEvent),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PositionEvent {
    pub x: i32,
    pub y: i32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MotionEvent {
    pub dx: i32,
    pub dy: i32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GrabEvent {
    pub grab: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ButtonEvent {
    pub button: u32,
    pub state: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct KeyEvent {
    pub key: u64,
    pub state: bool,
}

#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub enum SelectionEvent {
    Primary,
    Clipboard,
}