use jzon::{object, array, JsonValue};
use actix_web::{web, HttpRequest, Responder};

use crate::router::{global, items, userdata, databases};
use crate::encryption;

pub fn routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/chat")
            .route("/home", web::post().to(home))
            .route("/talk/start", web::post().to(start))
            .route("/talk/end", web::post().to(end))
    );
}

pub fn add_chat(id: i64, num: i64, chats: &mut JsonValue) -> bool {
    for data in chats.members() {
        if data["chat_id"] == id && data["room_id"] == num {
            return false;
        }
    }
    chats.push(object!{
        chat_id: id,
        room_id: num,
        chapter_id: databases::CHAPTERS[id.to_string()][num.to_string()]["id"].clone(),
        is_read: 0,
        created_at: global::timestamp()
    }).unwrap();
    true
}

pub fn add_chat_from_chapter_id(chapter_id: i64, chats: &mut JsonValue) -> bool {
    let chapter = &databases::CHAPTERS_MASTER[chapter_id.to_string()];
    if chapter.is_empty() {
        println!("Attempted to give unknown chapter id {}", chapter_id);
        return false;
    }
    add_chat(chapter["masterChatId"].as_i64().unwrap(), chapter["roomId"].as_i64().unwrap(), chats)
}

async fn home(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let chats = userdata::get_acc_chats(&key);
    
    let mut rooms = array![];
    for data in chats.members() {
        rooms.push(databases::CHATS[data["chat_id"].to_string()][data["room_id"].to_string()]["id"].clone()).unwrap();
    }
    
    global::api(&req, Some(object!{
        "progress_list": chats,
        "master_chat_room_ids": rooms,
        "master_chat_stamp_ids": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,43,44,45,46,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,11001003,22001001,33001001,44001002],
        "master_chat_attachment_ids": []
    }))
}

async fn start(req: HttpRequest, _body: String) -> impl Responder {
    global::api(&req, Some(object!{"select_talk_id_list":[],"get_item_list":[],"is_read":0}))
}

async fn end(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let body = jzon::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
    let mut missions = userdata::get_acc_missions(&key);
    let mut chats = userdata::get_acc_chats(&key);
    
    for data in chats.members_mut() {
        if body["chapter_id"].as_i64().unwrap() == data["chapter_id"].as_i64().unwrap() {
            if data["is_read"].as_i32().unwrap() != 1 {
                items::advance_mission(1169001, 1, 50, &mut missions);
            }
            data["is_read"] = 1.into();
            userdata::save_acc_missions(&key, missions);
            userdata::save_acc_chats(&key, chats);
            break;
        }
    }
    
    global::api(&req, Some(array![]))
}
