use data::tables::{self, ArchiveBattleQuestID};

use crate::logic::{
    game::{GameInstance, HollowGame, LogicError},
    ELocalPlayType, EQuestType,
};

use super::*;

pub async fn on_get_quest_data(
    _session: &NetSession,
    _player: &mut Player,
    req: GetQuestDataCsReq,
) -> NetResult<GetQuestDataScRsp> {
    Ok(GetQuestDataScRsp {
        retcode: Retcode::RetSucc.into(),
        quest_type: req.quest_type,
        quest_data: Some(QuestData {
            quest_collection_list: vec![
                QuestCollection {
                    quest_type: EQuestType::ArchiveFile as u32,
                    quest_id_list: tables::archive_file_quest_template_tb::iter()
                        .map(|tmpl| tmpl.id.value())
                        .collect(),
                    ..Default::default()
                },
                QuestCollection {
                    quest_type: EQuestType::Hollow as u32,
                    quest_id_list: tables::hollow_quest_template_tb::iter()
                        .map(|tmpl| tmpl.id.value())
                        .collect(),
                    ..Default::default()
                },
            ],
        }),
    })
}

pub async fn on_get_yorozuya_info(
    _session: &NetSession,
    _player: &mut Player,
    _req: GetYorozuyaInfoCsReq,
) -> NetResult<GetYorozuyaInfoScRsp> {
    Ok(GetYorozuyaInfoScRsp {
        retcode: Retcode::RetSucc.into(),
        yorozuya_info: Some(YorozuyaInfo {
            odohdljfdlf: vec![1000, 1001, 1002, 1003, 1004],
            apmojjlcooa: vec![1000, 1001, 1002, 1003, 1004],
            akiddbalfoa: vec![10010001, 10010002, 10010004],
            npgjhahijkb: tables::hollow_config_template_tb::iter()
                .map(|tmpl| tmpl.id.value())
                .collect(),
            eoljpdnjgeg: tables::hollow_quest_template_tb::iter()
                .map(|tmpl| Ofhlkjeakif {
                    nnkcanmllod: tmpl.id.value(),
                    kkjlnkehddj: Some(Cgpajijemlj::default()),
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        }),
    })
}

pub async fn on_get_archive_info(
    _session: &NetSession,
    _player: &mut Player,
    _req: GetArchiveInfoCsReq,
) -> NetResult<GetArchiveInfoScRsp> {
    Ok(GetArchiveInfoScRsp {
        retcode: Retcode::RetSucc.into(),
        archive_info: Some(ArchiveInfo {
            hollow_archive_id_list: (1..99999).collect(),
            videotaps_info: tables::archive_file_quest_template_tb::iter()
                .map(|tmpl| VideotapeInfo {
                    archive_file_id: tmpl.id.value(),
                    finished: true,
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        }),
    })
}

pub async fn on_begin_archive_battle_quest(
    session: &NetSession,
    player: &mut Player,
    req: BeginArchiveBattleQuestCsReq,
) -> NetResult<BeginArchiveBattleQuestScRsp> {
    let quest_id = ArchiveBattleQuestID::new(req.quest_id).ok_or(Retcode::RetFail)?;

    player.game_instance = GameInstance::Hollow(
        HollowGame::create_archive_battle(
            quest_id,
            ELocalPlayType::ArchiveBattle,
            &req.avatars,
            req.buddy_id,
        )
        .map_err(LogicError::from)?,
    );

    let world_init_notify = player.game_instance.create_world_init_notify()?;
    session.notify(world_init_notify).await?;

    Ok(BeginArchiveBattleQuestScRsp {
        retcode: Retcode::RetSucc.into(),
        quest_id: req.quest_id,
    })
}

pub async fn on_finish_archive_quest(
    _session: &NetSession,
    _player: &mut Player,
    req: FinishArchiveQuestCsReq,
) -> NetResult<FinishArchiveQuestScRsp> {
    Ok(FinishArchiveQuestScRsp {
        retcode: Retcode::RetSucc.into(),
        quest_id: req.quest_id,
        ..Default::default()
    })
}
