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

use crate::router::{databases, global, items, userdata};
use crate::encryption;
use crate::router::clear_rate::live_completed;
use crate::router::tools::guest;

pub fn routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/live")
            .route("/guest", web::post().to(guest))
            .route("/mission", web::post().to(mission))
            .route("/ranking", web::post().to(crate::router::clear_rate::ranking))
            .route("/clearRate", web::get().to(crate::router::clear_rate::clearrate))
            .route("/start", web::post().to(start))
            .route("/end", web::post().to(end))
            .route("/skip", web::post().to(skip))
            .route("/retire", web::post().to(retire))
            .route("/continue", web::post().to(continuee))
            .route("/reward", web::post().to(reward))
    );
}


async fn retire(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let body = jzon::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
    live_retire(&key, &body);
    if body["live_score"]["play_time"].as_i64().unwrap_or(0) > 5 {
        live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), true, 0, 0);
    }
    global::api(&req, Some(object!{
        "stamina": {},
        "item_list": [],
        "event_point_list": []
    }))
}

async fn reward(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 user = userdata::get_acc(&key);
    let live_id = body["master_live_id"].as_i64().unwrap();
    let cleared = get_clear_count(live_id, &user) > 0;

    let mut ensured_list = array![];
    let mut random_list = array![];
    for data in databases::CLEAR_REWARD.members() {
        if data["masterLiveId"].as_i64().unwrap() != live_id {
            continue;
        }
        if data["getableCount"].as_i64().unwrap() == 0 {
            continue;
        }
        let entry = object!{
            master_live_clear_reward_id: data["id"].clone(),
            type: data["type"].clone(),
            value: data["value"].clone(),
            amount: data["amount"].clone(),
            getable_count: data["getableCount"].clone(),
            received_count: if data["type"] == 1 && cleared { 1 } else { 0 }
        };
        if data["type"] == 1 {
            ensured_list.push(entry).unwrap();
        } else {
            random_list.push(entry).unwrap();
        }
    }

    global::api(&req, Some(object!{
        "ensured_list": ensured_list,
        "random_list": random_list
    }))
}

fn random_number(lowest: usize, highest: usize) -> usize {
    if lowest == highest {
        return lowest;
    }
    assert!(lowest < highest);
    
    rand::rng().random_range(lowest..highest + 1)
}

async fn guest(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let user_id = userdata::get_acc(&key)["user"]["id"].as_i64().unwrap();
    let friends = userdata::get_acc_friends(&key);
    let user = userdata::get_acc(&key);
    
    let mut guest_list = array![];
    if user["tutorial_step"].as_i32().unwrap() != 130 {
        guest_list.push(object!{
            "user": {
                "name": "A VERY Nice Guest",
                "comment": "Enjoy your first live show!",
                "exp": 900,
                "main_deck_slot": 1,
                "favorite_master_card_id": 10010013,
                "favorite_card_evolve": 0,
                "guest_smile_master_card_id": 10010013,
                "guest_cool_master_card_id": 10010013,
                "guest_pure_master_card_id": 10010013,
                "friend_request_disabled": 1,
                "master_title_ids": [3000001,0],
                "profile_settings": [1,2,3,4,5,6,7],
                "last_login_time": 1708699449
            },
            "favorite_card": {
                "id": 0,
                "master_card_id": 10010013,
                "exp": 1025,
                "skill_exp": 0,
                "evolve": []
            },
            "guest_smile_card": {
                "id": 0,
                "master_card_id": 10010013,
                "exp": 1025,
                "skill_exp": 0,
                "evolve": []
            },
            "guest_cool_card": {
                "id": 0,
                "master_card_id": 10010013,
                "exp": 1025,
                "skill_exp": 0,
                "evolve": []
            },
            "guest_pure_card": {
                "id": 0,
                "master_card_id": 10010013,
                "exp": 1025,
                "skill_exp": 0,
                "evolve": []
            },
            "status":0
        }).unwrap();
    } else {
        if !friends["friend_user_id_list"].is_empty() {
            guest_list.push(guest::get_user(friends["friend_user_id_list"][random_number(0, friends["friend_user_id_list"].len() - 1)].as_i64().unwrap(), &friends, guest::UserView::Card)).unwrap();
        }
        let expected: usize = 5;
        if guest_list.len() < expected {
            let mut random = userdata::get_random_uids((expected-guest_list.len()) as i32);
            let index = random.members().position(|r| *r.to_string() == user_id.to_string());
            if index.is_some() {
                random.array_remove(index.unwrap());
            }
            
            for uid in random.members() {
                let guest = guest::get_user(uid.as_i64().unwrap(), &friends, guest::UserView::Card);
                if guest["user"]["friend_request_disabled"] == 1 || guest.is_empty() {
                    continue;
                }
                guest_list.push(guest).unwrap();
            }
        }
        if guest_list.is_empty() {
            guest_list.push(object!{
                "user": {
                    "name": "A sad Guest",
                    "comment": "Cant believe you're the only person on this server!",
                    "exp": 900,
                    "main_deck_slot": 1,
                    "favorite_master_card_id": 10010013,
                    "favorite_card_evolve": 0,
                    "guest_smile_master_card_id": 10010013,
                    "guest_cool_master_card_id": 10010013,
                    "guest_pure_master_card_id": 10010013,
                    "friend_request_disabled": 1,
                    "master_title_ids": [3000001,0],
                    "profile_settings": [1,2,3,4,5,6,7],
                    "last_login_time": 1708699449
                },
                "favorite_card": {
                    "id": 0,
                    "master_card_id": 10010013,
                    "exp": 1025,
                    "skill_exp": 0,
                    "evolve": []
                },
                "guest_smile_card": {
                    "id": 0,
                    "master_card_id": 10010013,
                    "exp": 1025,
                    "skill_exp": 0,
                    "evolve": []
                },
                "guest_cool_card": {
                    "id": 0,
                    "master_card_id": 10010013,
                    "exp": 1025,
                    "skill_exp": 0,
                    "evolve": []
                },
                "guest_pure_card": {
                    "id": 0,
                    "master_card_id": 10010013,
                    "exp": 1025,
                    "skill_exp": 0,
                    "evolve": []
                },
                "status":0
            }).unwrap();
        }
    }
    
    global::api(&req, Some(object!{
        "guest_list": guest_list
    }))
}

async fn mission(req: HttpRequest, _body: String) -> impl Responder {
    //todo
    global::api(&req, Some(object!{
        "score_ranking": "",
        "combo_ranking": "",
        "clear_count_ranking": ""
    }))
}

fn check_for_stale_data(server_data: &mut JsonValue, live_id: i64) {
    let mut expired = array![];
    let curr_time = global::timestamp();
    for (i, live) in server_data["last_live_started"].members().enumerate() {
        if live["expire_date_time"].as_u64().unwrap() < curr_time || live["master_live_id"] == live_id {
            expired.push(i).unwrap();
        }
        if live["expire_date_time"].as_u64().unwrap() < curr_time {
            // User closed game after losing. Count this as a fail.
            live_completed(live["master_live_id"].as_i64().unwrap(), live["level"].as_i32().unwrap(), true, 0, 0);
        }
    }
    for i in expired.members() {
        server_data["last_live_started"].array_remove(i.as_usize().unwrap());
    }
}

fn get_end_live_deck_id(login_token: &str, body: &JsonValue) -> Option<i32> {
    let mut server_data = userdata::get_server_data(login_token);
    if server_data["last_live_started"].is_null() {
        server_data["last_live_started"] = array![];
    }
    let index = server_data["last_live_started"].members().position(|r| r["master_live_id"] == body["master_live_id"])?;
    let rv = server_data["last_live_started"][index]["deck_slot"].as_i32()?;

    check_for_stale_data(&mut server_data, body["master_live_id"].as_i64().unwrap());
    userdata::save_server_data(login_token, server_data);
    Some(rv)
}

pub fn get_end_live_event_id(login_token: &str, body: &JsonValue) -> Option<u32> {
    let server_data = userdata::get_server_data(login_token);
    if server_data["last_live_started"].is_null() {
        return None;
    }
    let index = server_data["last_live_started"].members().position(|r| r["master_live_id"] == body["master_live_id"])?;
    let rv = server_data["last_live_started"][index]["master_event_id"].as_u32()?;

    Some(rv)
}

fn live_retire(login_token: &str, body: &JsonValue) {
    let mut server_data = userdata::get_server_data(login_token);
    check_for_stale_data(&mut server_data, body["master_live_id"].as_i64().unwrap());
    userdata::save_server_data(login_token, server_data);
}

fn start_live(login_token: &str, body: &JsonValue) {
    let mut server_data = userdata::get_server_data(login_token);
    if server_data["last_live_started"].is_null() {
        server_data["last_live_started"] = array![];
    }
    check_for_stale_data(&mut server_data, body["master_live_id"].as_i64().unwrap());
    let mut to_save = body.clone();
    // The user has 1 hour to complete a live
    to_save["expire_date_time"] = (global::timestamp() + (1 * 60 * 60)).into();
    server_data["last_live_started"].push(to_save).unwrap();

    userdata::save_server_data(login_token, server_data);
}

async fn start(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let body = jzon::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
    start_live(&key, &body);
    global::api(&req, Some(array![]))
}

pub async fn event_start(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let body = jzon::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
    start_live(&key, &body);
    global::api(&req, Some(array![]))
}

async fn continuee(req: HttpRequest, body: String) -> impl Responder {
    let key = global::get_login(req.headers(), &body);
    let mut user = userdata::get_acc(&key);
    
    items::remove_gems(&mut user, 100);
    
    userdata::save_acc(&key, user.clone());
    
    global::api(&req, Some(object!{
        gem: user["gem"].clone()
    }))
}

fn get_clear_count(id: i64, user: &JsonValue) -> i64 {
    let mut rv = 0;
    for current in user["live_list"].members() {
        if current["master_live_id"] == id {
            rv += current["clear_count"].as_i64().unwrap();
        }
    }
    rv
}

pub fn update_live_data(user: &mut JsonValue, data: &JsonValue, add: bool) -> JsonValue {
    if user["tutorial_step"].as_i32().unwrap() < 130 {
        return JsonValue::Null;
    }
    
    let mut rv = object!{
        "master_live_id": data["master_live_id"].as_i64().unwrap(),
        "level": data["level"].as_i64().unwrap(),
        "clear_count": 1,
        "high_score": data["live_score"]["score"].as_i64().unwrap(),
        "max_combo": data["live_score"]["max_combo"].as_i64().unwrap(),
        "auto_enable": 1, //whats this?
        "updated_time": global::timestamp()
    };
    
    let mut has = false;
    for current in user["live_list"].members_mut() {
        if current["master_live_id"] == rv["master_live_id"] && (current["level"] == rv["level"] || data["level"].as_i32().unwrap() == 0) {
            has = true;
            if add {
                rv["clear_count"] = (current["clear_count"].as_i64().unwrap() + 1).into();
            }
            current["clear_count"] = rv["clear_count"].clone();
            
            if rv["high_score"].as_i64().unwrap() > current["high_score"].as_i64().unwrap() {
                current["high_score"] = rv["high_score"].clone();
            } else {
                rv["high_score"] = current["high_score"].clone();
            }
            
            if rv["max_combo"].as_i64().unwrap() > current["max_combo"].as_i64().unwrap() {
                current["max_combo"] = rv["max_combo"].clone();
            } else {
                rv["max_combo"] = current["max_combo"].clone();
            }
            current["updated_time"] = rv["updated_time"].clone();
            rv["level"] = current["level"].clone();
            if data["level"].as_i32().unwrap() != 0 {
                break;
            }
        }
    }
    if !has {
        user["live_list"].push(rv.clone()).unwrap()
    }
    rv
}
pub fn update_live_mission_data(user: &mut JsonValue, data: &JsonValue) {
    if user["tutorial_step"].as_i32().unwrap() < 130 {
        return;
    }
    
    let rv = object!{
        "master_live_id": data["master_live_id"].as_i64().unwrap(),
        "clear_master_live_mission_ids": data["clear_master_live_mission_ids"].clone()
    };
    
    for current in user["live_mission_list"].members_mut() {
        if current["master_live_id"] == rv["master_live_id"] {
            for id in data["clear_master_live_mission_ids"].members() {
                if !current["clear_master_live_mission_ids"].contains(id.as_i32().unwrap()) {
                    current["clear_master_live_mission_ids"].push(id.as_i32().unwrap()).unwrap();
                }
            }
            return;
        }
    }
    user["live_mission_list"].push(rv.clone()).unwrap();
}

fn get_live_mission_completed_ids(user: &JsonValue, live_id: i64, score: i64, combo: i64, clear_count: i64, level: i64, full_combo: bool, all_perfect: bool) -> Option<JsonValue> {
    let live_info = &databases::LIVE_LIST[live_id.to_string()];
    let mut out = array![];
    let combo_info = &databases::MISSION_COMBO_DATA[live_info["masterMusicId"].to_string()];
    
    for data in databases::MISSION_DATA.members() {
        match data["type"].as_i32()? {
            1 => {
                if live_info[&format!("score{}", data["value"])].as_i64()? <= score {
                    out.push(data["id"].as_i32()?).ok()?;
                }
            },
            2 => {
                if combo_info["valueList"][data["value"].to_string().parse::<usize>().ok()?].as_i64()? <= combo {
                    out.push(data["id"].as_i32()?).ok()?;
                }
            },
            3 => {
                if full_combo && data["level"].as_i64()? == level {
                    out.push(data["id"].as_i32()?).ok()?;
                }
            },
            4 => {
                if clear_count >= data["value"].to_string().parse::<i64>().ok()? {
                    out.push(data["id"].as_i32()?).ok()?;
                }
            },
            5 => {
                if full_combo && all_perfect && data["level"].as_i64()? == level {
                    out.push(data["id"].as_i32()?).ok()?;
                }
            },
            _ => {}
        }
    }
    let mut rv = array![];
    for current in user["live_mission_list"].members() {
        if current["master_live_id"] == live_id {
            for id in out.members() {
                if !current["clear_master_live_mission_ids"].contains(id.as_i32().unwrap()) {
                    rv.push(id.as_i32().unwrap()).unwrap();
                }
            }
            return Some(rv);
        }
    }
    Some(out)
}

fn mission_reward_reason(mission_type: i64, jp: bool) -> &'static str {
    match mission_type {
        1 => if jp { "楽曲のスコアランク達成報酬です。" } else { "Song Score Rank reward." },
        2 => if jp { "楽曲のコンボ達成報酬です。" } else { "Song Combo reward." },
        3 => if jp { "楽曲のフルコンボ達成報酬です。" } else { "Song Full Combo reward." },
        _ => if jp { "楽曲のクリア回数達成報酬です。" } else { "Song Clear Count reward." },
    }
}

fn give_mission_rewards(user: &mut JsonValue, home: &mut JsonValue, missions: &JsonValue, user_missions: &mut JsonValue, cleared_missions: &mut JsonValue, chats: &mut JsonValue, multiplier: i64, jp: bool) -> JsonValue {
    let mut rv = array![];
    for data in databases::MISSION_DATA.members() {
        if !missions.contains(data["id"].as_i32().unwrap()) {
            continue;
        }
        if data["masterLiveMissionRewardId"].as_i64().unwrap() == 0 {
            continue;
        }
        let reward = databases::MISSION_REWARD_DATA[data["masterLiveMissionRewardId"].to_string()].clone();
        let reason = mission_reward_reason(data["type"].as_i64().unwrap_or(0), jp);
        items::gift_item_basic(reward["value"].as_i32().unwrap(), reward["amount"].as_i64().unwrap(), reward["type"].as_i32().unwrap(), reason, home);
    }
    if items::give_gift_basic(3, 16005001, 5 * multiplier, user, user_missions, cleared_missions, chats) {
        rv.push(object!{"type":3,"value":16005001,"level":0,"amount":5 * multiplier}).unwrap();
    }
    if items::give_gift_basic(3, 17001001, 1, user, user_missions, cleared_missions, chats) {
        rv.push(object!{"type":3,"value":17001001,"level":0,"amount":1}).unwrap();
    }
    rv
}
pub fn get_master_num(id: i64) -> i64 {
    let id = id.to_string();
    let mut masterid = 0;
    if id.starts_with('2') {
        masterid += 9;
    } else if id.starts_with('3') {
        masterid += 9 + 9;
    } else if id.starts_with('4') {
        masterid += 9 + 9 + 12;
    }
    masterid
}

pub fn get_master_id(id: i64) -> i64 {
    get_master_num(id) + id.to_string().char_indices().last().unwrap().1.to_string().parse::<i64>().unwrap()
}

const MAX_BOND: i64 = 500000;

lazy_static! {
    static ref BOND_WEIGHT: JsonValue = {
        array![1, 1, 2, 2, 5, 2, 2, 1, 1]
    };
    static ref CHATS: JsonValue = {
        array![1, 1000, 2000, 3500, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12500, 13000, 13500, 15000]
    };
}

fn character_bond(character_list: &JsonValue, character: i64) -> i64 {
    for c in character_list.members() {
        if c["master_character_id"].as_i64() == Some(character) {
            return c["exp"].as_i64().unwrap_or(0);
        }
    }
    0
}

fn set_character_bond(character_list: &mut JsonValue, character: i64, exp: i64) {
    for c in character_list.members_mut() {
        if c["master_character_id"].as_i64() == Some(character) {
            c["exp"] = exp.into();
            return;
        }
    }
    character_list.push(object!{
        master_character_id: character,
        exp: exp
    }).unwrap();
}

fn get_live_character_list(lp_used: i32, deck_id: i32, user: &mut JsonValue, missions: &mut JsonValue, completed_missions: &mut JsonValue, chats: &mut JsonValue) -> JsonValue {
    let card_list = user["card_list"].clone();
    let deck = user["deck_list"][(deck_id - 1) as usize]["main_card_ids"].clone();
    let tutorial_step = user["tutorial_step"].as_i32().unwrap();
    let mut character_list = user["character_list"].clone();

    // [1,1,2,2,5,2,2,1,1]
    let mut chars: Vec<i64> = vec![];
    let mut gains: Vec<i64> = vec![];
    for data in card_list.members() {
        let id = data["id"].as_i64().unwrap_or(0);
        let mcid = data["master_card_id"].as_i64().unwrap_or(0);
        let index = match deck.members().position(|r| r.as_i64() == Some(id) || r.as_i64() == Some(mcid)) {
            Some(x) => x,
            None => continue
        };
        let character = match databases::CARD_LIST[mcid.to_string()]["masterCharacterId"].as_i64() {
            Some(c) => c,
            None => continue
        };
        // Tutorial lives award no bond.
        let weight = if tutorial_step >= 130 {
            BOND_WEIGHT[index].as_i64().unwrap_or(0) * (lp_used as i64 / 10)
        } else {
            0
        };
        if let Some(p) = chars.iter().position(|c| *c == character) {
            gains[p] += weight;
        } else {
            chars.push(character);
            gains.push(weight);
        }
    }

    let mut rv = array![];
    for (idx, &character) in chars.iter().enumerate() {
        let before = character_bond(&character_list, character);
        let after = (before + gains[idx]).min(MAX_BOND);
        set_character_bond(&mut character_list, character, after);

        if gains[idx] > 0 {
            let mut mission_id = 1158000 + get_master_id(character);
            let mut limit = 1500;
            let mut status = items::get_mission_status(mission_id, missions);
            if status.is_empty() {
                mission_id += 39;
                limit *= 10;
                status = items::get_mission_status(mission_id, missions);
            }
            if status["status"].as_i32().unwrap_or(0) <= 1 {
                let completed = after >= limit;
                if let Some(m) = items::update_mission_status(mission_id, 0, completed, false, gains[idx], missions) {
                    completed_missions.push(m).unwrap();
                }
            }
        }

        rv.push(object!{
            master_character_id: character,
            exp: after,
            before_exp: before
        }).unwrap();
    }

    if tutorial_step >= 130 {
        for data in rv.members() {
            for chat in CHATS.members() {
                if chat.as_i64().unwrap() > data["exp"].as_i64().unwrap() {
                    break;
                }
                if crate::router::chat::add_chat(data["master_character_id"].as_i64().unwrap(), 1, chats) {
                    let mission_id = 1958000 + (get_master_id(data["master_character_id"].as_i64().unwrap()) * CHATS.len() as i64);
                    items::update_mission_status(mission_id, 0, true, true, 1, missions);
                    completed_missions.push(mission_id).unwrap();
                }
            }
        }
    }

    user["character_list"] = character_list;
    rv
}

pub fn live_end(req: &HttpRequest, body: &str, skipped: bool) -> JsonValue {
    let key = global::get_login(req.headers(), body);
    let body = jzon::parse(&encryption::decrypt_packet(body).unwrap()).unwrap();
    let mut user2 = userdata::get_acc_home(&key);
    let mut user = userdata::get_acc(&key);
    let mut user_missions = userdata::get_acc_missions(&key);
    let mut chats = userdata::get_acc_chats(&key);

    let jp = items::get_region(req.headers());
    let first_clear = !skipped
        && user["tutorial_step"].as_i32().unwrap() >= 130
        && get_clear_count(body["master_live_id"].as_i64().unwrap(), &user) == 0;

    let live = if skipped {
        items::use_item(&object!{
            value: 21000001,
            amount: 1,
            consumeType: 4
        }, body["live_boost"].as_i64().unwrap(), &mut user);
        update_live_data(&mut user, &object!{
            master_live_id: body["master_live_id"].clone(),
            level: 0,
            live_score: {
                score: 1,
                max_combo: 1
            }
        }, false)
    } else {
        update_live_data(&mut user, &body, true)
    };
    
    //1273009, 1273010, 1273011, 1273012
    let mut cleared_missions = items::advance_variable_mission(1105001, 1105017, 1, &mut user_missions);
    if body["master_live_id"].to_string().len() > 1 {
        let id = body["master_live_id"].to_string().split("").collect::<Vec<_>>()[2].parse::<i64>().unwrap_or(0);
        if (1..=4).contains(&id) {
            let to_push = items::completed_daily_mission(1273009 + id - 1, &mut user_missions);
            for data in to_push.members() {
                cleared_missions.push(data.as_i32().unwrap()).unwrap();
            }
        }
    }
    
    let missions;
    if skipped {
        live_completed(body["master_live_id"].as_i64().unwrap(), live["level"].as_i32().unwrap(), false, live["high_score"].as_i64().unwrap(), user["user"]["id"].as_i64().unwrap());
        let clear_count = get_clear_count(body["master_live_id"].as_i64().unwrap(), &user);

        missions = get_live_mission_completed_ids(&user, body["master_live_id"].as_i64().unwrap(), live["high_score"].as_i64().unwrap(), live["max_combo"].as_i64().unwrap(), clear_count, live["level"].as_i64().unwrap(), false, false).unwrap_or(array![]);
    } else {
        live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), false, body["live_score"]["score"].as_i64().unwrap(), user["user"]["id"].as_i64().unwrap());
        let clear_count = get_clear_count(body["master_live_id"].as_i64().unwrap(), &user);

        let is_full_combo = (body["live_score"]["good"].as_i32().unwrap_or(1) + body["live_score"]["bad"].as_i32().unwrap_or(1) + body["live_score"]["miss"].as_i32().unwrap_or(1)) == 0;

        let is_perfect = is_full_combo && body["live_score"]["great"].as_i32().unwrap_or(1) == 0;

        missions = get_live_mission_completed_ids(&user, body["master_live_id"].as_i64().unwrap(), body["live_score"]["score"].as_i64().unwrap(), body["live_score"]["max_combo"].as_i64().unwrap(), clear_count, body["level"].as_i64().unwrap(), is_full_combo, is_perfect).unwrap_or(array![]);
    
        if is_full_combo {
            if items::advance_mission(1176001, 1, 1, &mut user_missions).is_some() {
                cleared_missions.push(1176001).unwrap();
            }
            if items::advance_mission(1176002, 1, 100, &mut user_missions).is_some() {
                cleared_missions.push(1176002).unwrap();
            }
            if items::advance_mission(1176003, 1, 200, &mut user_missions).is_some() {
                cleared_missions.push(1176003).unwrap();
            }
            if items::advance_mission(1176004, 1, 300, &mut user_missions).is_some() {
                cleared_missions.push(1176004).unwrap();
            }
            if items::advance_mission(1176005, 1, 400, &mut user_missions).is_some() {
                cleared_missions.push(1176005).unwrap();
            }
            if items::advance_mission(1176006, 1, 500, &mut user_missions).is_some() {
                cleared_missions.push(1176006).unwrap();
            }
        }
        if is_perfect && items::advance_mission(1177001, 1, 1, &mut user_missions).is_some() {
            cleared_missions.push(1177001).unwrap();
        }
        if is_perfect && body["level"].as_i32().unwrap() == 4 && items::advance_mission(1177002, 1, 1, &mut user_missions).is_some() {
            cleared_missions.push(1177002).unwrap();
        }
    }
    
    update_live_mission_data(&mut user, &object!{
        master_live_id: body["master_live_id"].as_i64().unwrap(),
        clear_master_live_mission_ids: missions.clone()
    });
    
    let lp_used: i32 = body["use_lp"].as_i32().unwrap_or(10 * body["live_boost"].as_i32().unwrap_or(0));

    let mut reward_list = give_mission_rewards(&mut user, &mut user2, &missions, &mut user_missions, &mut cleared_missions, &mut chats, (lp_used / 10) as i64, jp);

    items::lp_modification(&mut user, lp_used as u64, true);

    items::give_exp(lp_used, &mut user, &mut user_missions, &mut cleared_missions);

    items::give_points(1, 75 * lp_used as i64, &mut user, &mut user_missions, &mut cleared_missions);

    if first_clear {
        items::give_primogems(60, &mut user);
        user["gem"]["total"] = (user["gem"]["charge"].as_i64().unwrap() + user["gem"]["free"].as_i64().unwrap()).into();
        reward_list.push(object!{
            type: 1,
            value: 1,
            level: 0,
            amount: 60,
            drop_info: {
                first_reward: 1,
                getable_count: 1,
                remaining_getable_count: 0
            }
        }).unwrap();
    }

    let deck_slot = get_end_live_deck_id(&key, &body).unwrap_or(body["deck_slot"].as_i32().unwrap_or(user["user"]["main_deck_slot"].as_i32().unwrap()));
    let characters = get_live_character_list(lp_used, deck_slot, &mut user, &mut user_missions, &mut cleared_missions, &mut chats);

    userdata::save_acc(&key, user.clone());
    userdata::save_acc_home(&key, user2.clone());
    userdata::save_acc_missions(&key, user_missions);
    userdata::save_acc_chats(&key, chats);

    let mut rv = object!{
        "item_list": user["item_list"].clone(),
        "point_list": user["point_list"].clone(),
        "live": live.clone(),
        "clear_master_live_mission_ids": missions,
        "user": user["user"].clone(),
        "stamina": user["stamina"].clone(),
        "character_list": characters,
        "reward_list": reward_list,
        "gift_list": user2["home"]["gift_list"].clone(),
        "clear_mission_ids": cleared_missions,
        "event_point_reward_list": [],
        "ranking_change": [],
        "event_member": [],
        "event_ranking_data": []
    };
    if first_clear {
        rv["gem"] = user["gem"].clone();
    }
    if skipped {
        rv["high_score"] = live["high_score"].clone();
    }
    rv
}

async fn end(req: HttpRequest, body: String) -> impl Responder {
    global::api(&req, Some(live_end(&req, &body, false)))
}

async fn skip(req: HttpRequest, body: String) -> impl Responder {
    global::api(&req, Some(live_end(&req, &body, true)))
}
