use {
    crate::{primary_writer::PrimaryWriter, writers::LogWriter, FlexiLoggerError},
    std::{
        collections::HashMap,
        sync::{
            mpsc::{channel, Receiver, Sender},
            Arc,
        },
        thread::Builder as ThreadBuilder,
    },
};

#[cfg(feature = "async")]
use {
    crate::{
        primary_writer::std_stream::StdStream,
        util::{eprint_err, ErrorCode, ASYNC_FLUSH, ASYNC_SHUTDOWN},
    },
    crossbeam_channel::Receiver as CrossbeamReceiver,
    crossbeam_queue::ArrayQueue,
    std::{sync::Mutex, thread::JoinHandle},
};

#[cfg(feature = "async")]
#[cfg(test)]
use std::io::Write;

#[cfg(feature = "async")]
const ASYNC_STD_WRITER: &str = "flexi_logger-std-writer";
const FLUSHER: &str = "flexi_logger-flusher";

// Used in Logger
pub(crate) fn start_flusher_thread(
    primary_writer: Arc<PrimaryWriter>,
    other_writers: Arc<HashMap<String, Box<dyn LogWriter>>>,
    flush_interval: std::time::Duration,
    #[cfg(feature = "affinity")] o_core_id: Option<usize>,
) -> Result<(), FlexiLoggerError> {
    let builder = ThreadBuilder::new().name(FLUSHER.to_string());
    #[cfg(not(feature = "dont_minimize_extra_stacks"))]
    let builder = builder.stack_size(1024);

    builder.spawn(move || {
        #[cfg(feature = "affinity")]
        bind_to_core(o_core_id);
        let (_sender, receiver): (Sender<()>, Receiver<()>) = channel();
        loop {
            receiver.recv_timeout(flush_interval).ok();
            primary_writer.flush().ok();
            for w in other_writers.values() {
                w.flush().ok();
            }
        }
    })?;
    Ok(())
}

#[cfg(feature = "async")]
pub(crate) fn start_async_stdwriter(
    mut std_stream: StdStream,
    receiver: CrossbeamReceiver<std::vec::Vec<u8>>,
    t_pool: Arc<ArrayQueue<Vec<u8>>>,
    msg_capa: usize,
    o_core_id: Option<usize>,
    #[cfg(test)] t_validation_buffer: Arc<Mutex<std::io::Cursor<Vec<u8>>>>,
) -> Mutex<Option<JoinHandle<()>>> {
    Mutex::new(Some(
        ThreadBuilder::new()
            .name(
                ASYNC_STD_WRITER.to_string()
            )
            .spawn(move || {
                bind_to_core(o_core_id);
                loop {
                    match receiver.recv() {
                        Err(_) => break,
                        Ok(mut message) => {
                            match message.as_ref() {
                                ASYNC_FLUSH => {
                                    std_stream
                                        .deref_mut()
                                        .flush()
                                        .unwrap_or_else(
                                            |e| eprint_err(ErrorCode::Flush, "flushing failed", &e)
                                        );
                                }
                                ASYNC_SHUTDOWN => {
                                    break;
                                }
                                _ => {
                                    std_stream
                                        .deref_mut()
                                        .write_all(&message)
                                        .unwrap_or_else(
                                            |e| eprint_err(ErrorCode::Write,"writing failed", &e)
                                        );
                                    #[cfg(test)]
                                    if let Ok(mut guard) = t_validation_buffer.lock() {
                                        (*guard).write_all(&message).ok();
                                    }
                                }
                            }
                            if message.capacity() <= msg_capa {
                                message.clear();
                                t_pool.push(message).ok();
                            }
                        }
                    }
                }
            })
            .unwrap(/* yes, let's panic if the thread can't be spawned */),
    ))
}

#[cfg(feature = "affinity")]
pub(crate) fn bind_to_core(o_core_id: Option<usize>) {
    if let Some(id) = o_core_id {
        if !core_affinity::set_for_current(core_affinity::CoreId { id }) {
            crate::util::eprint_msg(
                ErrorCode::BindToCore,
                &format!(
                    "flexi_logger: Warning: Could not bind thread {:?} to core {}",
                    std::thread::current().name(),
                    id,
                ),
            );
        }
    }
}
