use crate::ui::prelude::*;
use super::*;

pub fn schema(
) -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
    let menu_commands_handler = dptree::entry().branch(
        Message::filter_text()
            .branch(
                filter_localized_text!(Ru, "🎫Создать для Траффика")
                    .endpoint(create_traffic_promo_help_message),
            )
            .branch(
                filter_localized_text!(Ru, "🗑️Удалить промокоды")
                    .endpoint(delete_promos_handler),
            )
            .branch(
                filter_localized_text!(Ru, "🔙 Назад в панель")
                    .endpoint(support_entrypoint),
            )
            .branch(
                filter_localized_text!(Ru, "🎫 Промокоды")
                    .endpoint(show_promo_menu),
            )
    );

    use dptree::case;
    let message_handler = Update::filter_message()
        .branch(menu_commands_handler)
        .branch(case_expr![MasterState { support_kind: SupportStateKind::Menu, .. }])
        .branch(
            case_expr![MasterState { support_kind: SupportStateKind::StatsReceiveUserId, .. }]
                .endpoint(support_profile_handler),
        )
        .branch(
            case_expr![MasterState { support_kind: SupportStateKind::ReceivePoolPrice { pool_type }, .. } => pool_type]
                .endpoint(receive_pool_price_handler),
        )
        .branch(
            case_expr![MasterState { support_kind: SupportStateKind::DeletePromos { page }, .. } => page]
                .endpoint(delete_promos_state_handler),
        )
        .chain(
            Update::filter_message()
            .chain(
                Message::filter_text()
                .inspect(|text: String| println!("msg: {:?}", text))
                .branch(
                    case_expr![MasterState { support_kind: SupportStateKind::AddTrafficReceiveAmount { user_id, pool }, .. } => (user_id, pool)]
                        .endpoint(add_traffic_receive_amount_handler),
                )
            )
        );

    let command_handler = Update::filter_message()
        .filter_command::<Command>()
        .branch(case![Command::Promo].endpoint(support_promo_handler))
        .branch(case![Command::Profile].endpoint(support_profile_handler));

    let callback_handler = Update::filter_callback_query()
        .filter(|q: CallbackQuery| {
            if let Some(data) = q.data {
                serde_json::from_str::<SupportCallback>(&data).is_ok()
            } else {
                false
            }
        })
        .endpoint(callback_handler);

    support_filter_async().chain(
        case_expr![MasterState { kind: MasterStateKind::Support, .. }]
            .branch(command_handler)
            .branch(message_handler)
            .branch(callback_handler),
    )
}