#![cfg_attr(
  all(not(debug_assertions), target_os = "windows"),
  windows_subsystem = "windows"
)]

use tauri::Manager; 
use serde::{Deserialize, Serialize};
use serde_json::{Result, Value, json};

#[derive(Serialize, Deserialize, Debug)]
struct ZEvent {
    sender: String,
    data: Option<String>
}

impl ZEvent {
    fn default() -> Self {
      ZEvent { sender: String::default(), data: None }
    } 
}


fn main() {
  tauri::Builder::default()
  .setup(move |app| {

    // let sp = app.get_window("splashscreen").unwrap();
    let main = app.get_window("main").unwrap();

    // sp.show().unwrap();

    app.listen_global("dom-loaded", move |event| {
      // let data = event.payload();

      let v = json!(
        {
          "sender": "haah",
          "data": {
            "id": 234421,
            "message": "测试消息"
          }
        }
      );

      if let Some(data) = event.payload() {
        
        let jdata: Value = serde_json::from_str(data).unwrap();
        let ev: Option<ZEvent> = serde_json::from_str(data).ok();
        // match serde_json::from_str(data) {
        //   Ok(ev) => {}
        //   _ => {}
        // } 
        println!("jdata is {}, ev is {:?}", jdata, ev);
        // return;

        // match ev.sender.as_str() {
        //   "discover/recommend" => {
        //     main.show().unwrap();
        //     println!("main is ready to show");
        //   }
        //   _ => {}
        // }

        if let Some(data) = jdata["data"].as_object() {
          
        }

        if let Some(sender) = jdata["sender"].as_str() {
          match sender {
              "discover/recommend" => {
                main.show().unwrap();
                println!("main is ready to show");
              }
              _ => {}
          }
        }
        
      }
    });

    Ok(())
  })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
