You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: selection.rs
    path of file: ./repos/amethyst/amethyst_ui/src
    
    the code of the file until where you have to start completion: use std::marker::PhantomData;

use amethyst_core::{
    ecs::{Entity, IntoQuery, ParallelRunnable, System, SystemBuilder},
    shrev::{EventChannel, ReaderId},
};
use amethyst_input::InputHandler;
use derive_new::new;
use serde::{Deserialize, Serialize};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};

use crate::{CachedSelectionOrderResource, UiEvent, UiEventType};

// TODO: If none selected and there is a Selectable in the World, select the lower ordered one automatically?

/// Component indicating that a Ui entity is selectable.
/// Generic Type:
/// - G: Selection Group. Used to determine which entities can be selected together at the same time.
#[derive(Debug, Serialize, Deserialize, new)]
pub struct Selectable<G> {
    /// The order in which entities are selected when pressing the `Tab` key or the "go to next" input action.
    pub order: u32,
    #[new(default)]
    /// A multi selection group. When multiple entities are in the same selection group, they can be selected at
    /// the same time by holding shift or control and clicking them.
    /// You can also select the first element, then hold shift and press the keyboard arrow keys.
    // TODO: Holding shift + arrow keys to select more.
    // TODO: Pressing the arrow keys could optionally be binded to change the selected ui element.
    pub multi_select_group: Option<G>,
    #[new(default)]
    /// Indicates if you can select multiple entities at once without having to press the shift or control key.
    pub auto_multi_select: bool,
    /// Indicates if this consumes the inputs. If enabled, all inputs (except Tab) will be ignored when the component is focused.
    /// For example, the arrow keys will not change the selected ui element.
    /// Example usage: Ui Editable Text.
    #[new(default)]
   
    
    fn build(mut self) -> Box<dyn ParallelRunnable> {
        Box::new(SystemBuilder::new("SelectionKeyboardSystem")
            .read_resource::<EventChannel<Event<'static, ()>    >>()
            .read_resource::<CachedSelectionOrderResource>()
            .write_resource::<EventChannel<UiEvent>>()
            .read_resource::<InputHandler>()
            .with_query(<(Entity, &mut Selected)>::query())
            .build(move |commands, world,
                         ( window_events, cached,ui_events, inputs),
                         selected_query| {
                /*
                       Algorithm in use:

                       Add clicked elements + shift + ctrl status.
                       If tab or shift-tab
                           remove clicked buf
                           add replace: select higher or lower id closes to previous highest old id
                       if clicked buf isn't empty
                           if check currently highest selected multiselect group
                               // if shift && ctrl -> shift only
                               if shift
                                   add multiple
                               else if ctrl ||Â auto_multi_select
                                   add single
                               else
                                   add replace
                           else
                               add replace
                       */
                // Checks if tab was pressed.
                // TODO: Controller support/Use InputEvent in addition to keys.
                for event in window_events.read(&mut self.window_reader_id) {
                    if let Event::WindowEvent {
                        event:
                        WindowEvent::KeyboardInput {
                            input:
                            KeyboardInput {
                                state: ElementState::Pressed,
                                virtual_keycode: Some(VirtualKeyCode::Tab),
                                ..
                            },
                            ..
                        },
                        ..
                    } = *event
                    {
                        // Get index of highest selected ui element
                        let highest = cached.highest_order_selected_index(selected_query.iter_mut(world));

                        if let Some(highest) = highest {
                            // If Some, an element was currently selected. We move the cursor to the next or previous element depending if Shift was pressed.
                            // Select Replace
                            selected_query.for_each_mut(world, |(entity, _)| {
                                ui_events.single_write(UiEvent::new(UiEventType::Blur, *entity));
                                commands.remove_component::<Selected>(*entity);
                            });


                            let target = if !inputs.modifiers.shift() {
                                // Up
                                if highest > 0 {
                                    cached.cache.get(highest - 1).unwrap_or_else(|| cached.cache.last()
                                        .expect("unreachable: A highest ui element was selected, but none exist in the cache."))
                                } else {
                                    cached.cache.last()
                                        .expect("unreachable: A highest ui element was selected, but none exist in the cache.")
                                }
                            } else {
                                // Down
                                cached.cache.get(highest + 1).unwrap_or_else(|| cached.cache.first()
                                    .expect("unreachable: A highest ui element was selected, but none exist in the cache."))
                            };
                            commands.add_component(target.1, Selected);

                            ui_events.single_write(UiEvent::new(UiEventType::Focus, target.1));
                        } else if let Some(lowest) = cached.cache.first() {
                            // If None, nothing was selected. Try to take lowest if it exists.
                            commands.add_component(lowest.1, Selected);
                            ui_events.single_write(UiEvent::new(UiEventType::Focus, lowest.1));
                        }
                    }
                }
            })
        )
    }