import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
import 'package:yumi/shared/business_logic/models/res/room_res.dart';
import 'package:yumi/modules/user/fans/fans_user_list_page.dart';
import 'package:yumi/modules/settings/language/language_page.dart';
import 'package:yumi/modules/report/report_page.dart';
import 'package:yumi/modules/webview/webview_page.dart';

import 'package:yumi/app/routes/sc_router_init.dart';
import 'package:yumi/modules/admin/editing/sc_editing_user_room_page.dart';
import 'package:yumi/modules/admin/search/sc_edit_room_search_admin_page.dart';
import 'package:yumi/modules/admin/search/sc_edit_user_search_admin_page.dart';
import 'package:yumi/modules/user/profile/person_detail_page.dart';
import 'package:yumi/modules/search/sc_search_page.dart';
import 'package:yumi/modules/media/image_preview_page.dart';
import 'package:yumi/modules/media/video_player_page.dart';
import 'package:yumi/modules/user/follow/follow_user_list_page.dart';
import 'package:yumi/modules/user/level/level_page.dart';
import 'package:yumi/modules/user/visitor/visitor_user_list_page.dart';

import '../chat/message/sc_message_page.dart';
import '../user/edit/edit_user_info_page2.dart';

class SCMainRoute implements SCIRouterProvider {
  static String report = '/main/report';
  static String person = '/main/person';
  static String edit = '/main/person/edit';
  static String follow = '/main/me/follow';
  static String vistors = '/main/me/vistors';
  static String fans = '/main/me/fans';
  static String mainSearch = '/main/mainSearch';
  static String language = '/main/language';
  static String levelList = '/level/levelList';
  static String videoPlayer = '/main/videoPlayer';
  static String imagePreview = '/main/imagePreview';
  static String message = '/main/messagePage';
  static String editRoomSearchAdmin = '/main/editRoomSearchAdmin';
  static String editUserSearchAdmin = '/main/editUserSearchAdmin';
  static String editingUserRoomAdmin = '/main/editingUserRoomAdmin';
  static String webViewPage = '/main/webViewPage';

  @override
  void initRouter(FluroRouter router) {
    router.define(
      report,
      handler: Handler(
        handlerFunc:
            (_, params) => ReportPage(
              type: params['type']!.first,
              tageId: params['tageId']!.first,
            ),
      ),
    );
    router.define(
      person,
      handler: Handler(
        handlerFunc:
            (_, params) => PersonDetailPage(
              isMe: params['isMe']!.first,
              tageId: params['tageId']!.first,
            ),
      ),
    );
    router.define(
      mainSearch,
      handler: Handler(handlerFunc: (_, params) => SearchPage()),
    );
    router.define(
      message,
      handler: Handler(
        handlerFunc: (_, Map<String, List<String>> params) {
          String? isFromRoom = params['isFromRoom']?.first;
          return SCMessagePage(isFromRoom: isFromRoom == "true");
        },
      ),
    );

    router.define(
      language,
      handler: Handler(handlerFunc: (_, params) => LanguagePage()),
    );
    router.define(
      edit,
      handler: Handler(handlerFunc: (_, params) => EditUserInfoPage2()),
    );
    router.define(
      follow,
      handler: Handler(handlerFunc: (_, params) => FollowUserListPage()),
    );
    router.define(
      vistors,
      handler: Handler(handlerFunc: (_, params) => VisitorUserListPage()),
    );
    router.define(
      fans,
      handler: Handler(handlerFunc: (_, params) => FansUserListPage()),
    );
    router.define(
      levelList,
      handler: Handler(handlerFunc: (_, params) => LevelPage()),
    );
    router.define(
      editRoomSearchAdmin,
      handler: Handler(handlerFunc: (_, params) => SCEditRoomSearchAdminPage()),
    );
    router.define(
      editUserSearchAdmin,
      handler: Handler(handlerFunc: (_, params) => SCEditUserSearchAdminPage()),
    );
    router.define(
      videoPlayer,
      handler: Handler(
        handlerFunc: (_, Map<String, List<String>> params) {
          String? rawUrl = params['videoUrl']?.first;
          String decodedVideoUrl =
              rawUrl != null ? Uri.decodeComponent(rawUrl) : "";
          return VideoPlayerPage(videoUrl: decodedVideoUrl);
        },
      ),
    );
    router.define(
      imagePreview,
      handler: Handler(
        handlerFunc: (_, Map<String, List<String>> params) {
          List<String> imageUrls = [];
          int initialIndex = 0;

          try {
            // 解析 imageUrls 参数（JSON 字符串）
            if (params['imageUrls'] != null &&
                params['imageUrls']!.isNotEmpty) {
              String encodedUrls = params['imageUrls']!.first;
              String decodedUrls = Uri.decodeComponent(encodedUrls);
              List<dynamic> urlList = jsonDecode(decodedUrls);
              imageUrls = urlList.map((item) => item.toString()).toList();
            }

            // 解析 initialIndex
            if (params['initialIndex'] != null &&
                params['initialIndex']!.isNotEmpty) {
              initialIndex = int.tryParse(params['initialIndex']!.first) ?? 0;
            }
          } catch (e) {
            print('解析图片预览参数失败: $e');
            // 可以返回错误页面或默认页面
          }

          return ImagePreviewPage(
            imageUrls: imageUrls,
            initialIndex: initialIndex,
          );
        },
      ),
    );

    router.define(
      editingUserRoomAdmin,
      handler: Handler(
        handlerFunc: (_, Map<String, List<String>> params) {
          String? type = params['type']?.first;
          String? profileJson = params['profile']?.first;
          SocialChatUserProfile? userProfile;
          SocialChatRoomRes? roomProfile;
          if (type == "User") {
            userProfile = SocialChatUserProfile.fromJson(
              jsonDecode(profileJson ?? ""),
            );
          } else {
            roomProfile = SocialChatRoomRes.fromJson(
              jsonDecode(profileJson ?? ""),
            );
          }

          return SCEditingUserRoomPage(
            type: type ?? "",
            userProfile: userProfile,
            roomProfile: roomProfile,
          );
        },
      ),
    );

    router.define(
      webViewPage,
      handler: Handler(
        handlerFunc: (_, params) {
          String? url = params['url']?.first;
          String decodedUrl = url != null ? Uri.decodeComponent(url) : "";
          return WebViewPage(
            title: params['title']?.first ?? "",
            url: decodedUrl,
            showTitle: params['showTitle']?.first ?? "",
          );
        },
      ),
    );
  }
}
