use log::info;
use sv_parser::{RefNode, SyntaxTree};
use tower_lsp::lsp_types::*;

use crate::{server::LspServer, sources::{LSPSupport, Source}};
use crate::core::scope_tree::parse::get_ident;
use crate::core::scope_tree::common::Scope;

pub fn document_highlight(
    server: &LspServer,
    token: &str,
    file: &Source,
    pos: Position,
    uri: &Url
) -> Option<Vec<DocumentHighlight>> {
    let scope_tree = server.srcs.scope_tree.read().ok()?;

    // use the byte_idx of the definition if possible, otherwise use the cursor
    let byte_idx = match scope_tree.as_ref()?.get_definition(token, file.text.pos_to_byte(&pos), uri) {
        Some(def) => def.byte_idx,
        None => file.text.pos_to_byte(&pos),
    };
    
    // TODO: 完成剩余部分
    
    None
}

/// return all identifiers in a syntax tree matching a given token
fn all_identifiers(syntax_tree: &SyntaxTree, token: &str) -> Vec<(String, usize)> {
    let mut idents: Vec<(String, usize)> = Vec::new();
    for node in syntax_tree {
        if let RefNode::Identifier(_) = node {
            
            let (ident, byte_idx) = get_ident(syntax_tree, node);
            if ident == token {
                idents.push((ident, byte_idx));
            }
        }
    }
    idents
}