use std::io::Write;
use std::fs::OpenOptions;
use std::time::{SystemTime,UNIX_EPOCH};
use piston_window::*;

const LOG_PATH:&str="./log.txt";
const FONT_PATH:&str="./assets/mingliu.ttc";
const MAX_MESSAGE:usize=10;

fn append_file(msg:&str){
    let mut file=OpenOptions::new()
                    .append(true)
                    .create(true)
                    .open(LOG_PATH)
                    .unwrap();
    let now22=SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs();
    let mut log_line=format!("[{}]{}\n",now22,msg);
    if let Err(e)=file.write_all(log_line.as_bytes()){
        eprintln!("{}",e)
    }
}
fn main(){
    let mut windows22:PistonWindow=WindowSettings::new("dfd", [640,600]).
    exit_on_esc(true)
    .build()
    .unwrap();


    let mut menu_items=[
        "eeeeeee",
        "sssssss",
        "qqqqqqq",
        "yyyyyyy",
    ];
    let mut glyphs=windows22.load_font(FONT_PATH).expect("讀取不到自行");
    let mut selectied_index:usize=1;
    let mut messages:Vec<_>=Vec::new();
    let mut init_msg=format!("{}",menu_items[selectied_index]);
    println!("{}",init_msg);
    append_file(&init_msg);
    messages.push(init_msg);
    while let Some(event) = windows22.next() {
        let mut selection_changed=false;
        
        if let Some(Button::Keyboard(key))=event.press_args(){
            match key {
                Key::Up=>{
                    if selectied_index >0 {
                        selectied_index-=1 ;
                        selection_changed=true;
                    }
                }
                Key::Down=>{
                    if selectied_index < menu_items.len() -1{
                        selectied_index +=1;
                        selection_changed=true;
                    }
                }
                Key::Return=>{
                    let mut msg=format!("{}",menu_items[selectied_index]);
                    println!("{}",msg);
                    append_file(&msg);
                    messages.push(msg);

                }
                Key::Escape=>{
                    let mut msg=format!("使用者離開");
                    println!("{}",msg);
                    append_file(&msg);
                    messages.push(msg);
                    break;
                }
                _=>{}
            }
        }
        if let Some(pos)=event.mouse_cursor_args(){
            let y =pos[1];
            let index = ((y-150.0)/40.0) as usize;
            if index < menu_items.len() && index !=selectied_index{
                selectied_index=index;
                selection_changed=true;
            }


        }
        if let  Some(Button::Mouse(MouseButton::Left))=event.press_args(){
            let mut msg=format!("{}",menu_items[selectied_index]);
                    println!("{}",msg);
                    append_file(&msg);
                    messages.push(msg);
        }
        if messages.len() > MAX_MESSAGE {
            messages.drain(0..(menu_items.len()-MAX_MESSAGE));
        }
        windows22.draw_2d(&event, |context22,graphics33,device22|{
            clear([0.0,0.0,0.0,1.0], graphics33);
            text::Text::new_color([1.0;4], 23).draw(
                "歡迎",
                 &mut glyphs,
                  &context22.draw_state,
                   context22.transform.trans(150.0, 40.0),
                    graphics33).unwrap();
                for (i,items) in menu_items.iter().enumerate(){
                let y=150.0+ i as f64 *25.0;
                if y>=150.0 {
                       let color = if i == selectied_index{
                        [1.0;4]
                       }else {
                        [0.6,0.6,0.6,1.0]
                       };
                    text::Text::new_color(color, 32).draw(
                        &items, &mut glyphs, 
                        &context22.draw_state,
                         context22.transform.trans(150.0, y)
                         , graphics33).unwrap();
                }
            }
            let base_y=20.0;
            for (i,msg) in messages.iter().enumerate(){
                let y = base_y + i as f64 *25.0;
                text::Text::new_color([0.6,0.6,0.6,1.0], 32).draw(
                    &msg,
                     &mut glyphs,
                      &context22.draw_state,
                       context22.transform.trans(50.0, y),
                        graphics33).unwrap();

            }
            glyphs.factory.encoder.flush(device22);
        });
    }


}
請修正錯誤