use std::future;
use std::sync::Arc;
#[allow(unused)]
use log::info;
use serde::{Deserialize, Serialize};
use tower_lsp::jsonrpc::Result;
use crate::server::Backend;

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DoPrimitivesJudgeParams {
    name: String
}

#[derive(Clone)]
pub struct DoPrimitivesJudgeApi;

impl <'a>tower_lsp::jsonrpc::Method<&'a Arc<Backend>, (DoPrimitivesJudgeParams, ), Result<bool>> for DoPrimitivesJudgeApi {
    type Future = future::Ready<Result<bool>>;

    fn invoke(&self, _server: &'a Arc<Backend>, _params: (DoPrimitivesJudgeParams, )) -> Self::Future {        
        let request_param = _params.0;
        let primitive_name = request_param.name;
        let is_primitive = do_primitives_judge(&primitive_name, &_server);
        future::ready(Ok(is_primitive))
    }
}

fn do_primitives_judge(
    name: &str,
    backend: &Arc<Backend>
) -> bool {
    let sources = &backend.server.srcs;
    let primitive_text = sources.primitive_text.clone();
    let primitive_map = primitive_text.name_to_text.read().unwrap();
    primitive_map.contains_key(name)
}


