use tklog::{
    infos,
    sync::Logger, Format, MODE,
};
use lazy_static::lazy_static;
use std::{
    // fmt::format,
    borrow::{ BorrowMut}, sync::{Arc, Mutex}
};

struct LOGERME{
    logger_rust: Arc<Mutex<Logger>>,
    logger_js:  Arc<Mutex<Logger>>,
}
lazy_static! {

    static ref LOGERGLOBLE: Arc<Mutex<LOGERME>> = Arc::new(Mutex::new(
        LOGERME{
        logger_rust:Arc::new( Mutex::new(Logger::new())),
        logger_js:  Arc::new( Mutex::new(Logger::new())),
     }
));

}
// pub static  LOGGER_RUST: Arc<Mutex<Logger>> =Arc::new( Mutex::new(Logger::new()));
// pub static  LOGGER_JS:  Arc<Mutex<Logger>> = Arc::new(Mutex::new(Logger::new()));




pub fn change_log_path(path: &str,string: &str) {
    if string=="RUST"{
        let loggerglobel = LOGERGLOBLE.lock().unwrap();
        let mut log=loggerglobel.logger_rust.lock().unwrap();
        log.set_cutmode_by_time(path, MODE::DAY, 10, false);      
    }else if string=="JS"{
        let loggerglobel=LOGERGLOBLE.lock().unwrap();
        let mut log=loggerglobel.logger_js.lock().unwrap();
        log.set_cutmode_by_time(path, MODE::DAY, 10, false);            
    }
}

pub fn initlog() {

    //let mut logger = LOGERGloble.lock().unwrap().LOGGER_RUST;
    let loggerglobel = LOGERGLOBLE.lock().unwrap();
    let mut log=loggerglobel.logger_rust.lock().unwrap();
    log.set_console(true).set_format(Format::Date|Format::Time).set_formatter("{time} {file}{level}:{message}\n")
    .set_cutmode_by_time("rust_log.log",MODE::DAY, 10, false);
   // let mut logger = LOGERGloble.lock().unwrap().LOGGER_JS;
   //let mut loggerglobel = LOGERGloble.lock().unwrap();
   let mut log=loggerglobel.logger_js.lock().unwrap();
   log.set_console(false).set_format(Format::Date|Format::Time).set_formatter("{time}:\t{message}\n")
    .set_cutmode_by_time("js_log.log",MODE::DAY, 10, false);

}

pub fn logtorust<T>(str:T)
where T: std::fmt::Display
{
    let loggerglobe=LOGERGLOBLE.lock().unwrap();
    let mut log=Arc::clone(&loggerglobe.logger_rust);

    let log1 = log.borrow_mut();
    infos!( log1, str);
}

pub fn logtojs<T>(str:T)
where T: std::fmt::Display
{
    let loggerglobe=LOGERGLOBLE.lock().unwrap();
    let mut log=Arc::clone(&loggerglobe.logger_js);

    let log1 = log.borrow_mut();
    infos!( log1, str);
}

