import 'dart:io';

import 'package:jdsdk/jdsdk.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'package:sharesdk_plugin/sharesdk_register.dart';
import 'package:zhiying_base_widget/models/app_config_model.dart';
import 'package:zhiying_base_widget/pages/about_us_page/about_us_page.dart';
import 'package:zhiying_base_widget/pages/account_delete_page/account_delete_page.dart';
import 'package:zhiying_base_widget/pages/bil_detail_page/bil_detail_page.dart';
import 'package:zhiying_base_widget/pages/brand_detail_page/brand_detail_page.dart';
import 'package:zhiying_base_widget/pages/brand_detail_page/brand_detail_page_skeleton.dart';
import 'package:zhiying_base_widget/pages/brand_page/brand_page_skeleton.dart';
import 'package:zhiying_base_widget/pages/brand_search_page/brand_search_page.dart';
import 'package:zhiying_base_widget/pages/feedback_page/feedback_page.dart';
import 'package:zhiying_base_widget/pages/feedback_page/feedback_record_page.dart';
import 'package:zhiying_base_widget/pages/goods_details_page/goods_details_page.dart';
import 'package:zhiying_base_widget/pages/home_page/home_page.dart';
import 'package:zhiying_base_widget/pages/hot_ranking_page/hot_ranking_page.dart';
import 'package:zhiying_base_widget/pages/invited_friends/invited_friends.dart';
import 'package:zhiying_base_widget/pages/launch_page/launch_page.dart';
import 'package:zhiying_base_widget/pages/main_page/main_page.dart';
import 'package:zhiying_base_widget/pages/message_settings_page/message_settings_page.dart';
import 'package:zhiying_base_widget/pages/mine_detail_page/mine_detail_page.dart';
import 'package:zhiying_base_widget/pages/orders_page/orders_page.dart';
import 'package:zhiying_base_widget/pages/search_page/item/search_item_page.dart';
import 'package:zhiying_base_widget/pages/search_result_page/item/search_result_item_page.dart';
import 'package:zhiying_base_widget/pages/search_result_page/search_result_page.dart';
import 'package:zhiying_base_widget/pages/security_page/security_bind_alipay/security_bind_alipay_page.dart';
import 'package:zhiying_base_widget/pages/security_page/security_mobile/security_mobile.dart';
import 'package:zhiying_base_widget/pages/security_page/security_page.dart';
import 'package:zhiying_base_widget/pages/security_page/security_password/security_password.dart';
import 'package:zhiying_base_widget/pages/setting_page/setting_page.dart';
import 'package:zhiying_base_widget/pages/team_details_page/team_details_page.dart';
import 'package:zhiying_base_widget/pages/team_page/team_page.dart';
import 'package:zhiying_base_widget/pages/webview/base_webview.dart';
import 'package:zhiying_base_widget/pages/wechat_teacher_page/wechat_teacher_page.dart';
import 'package:zhiying_base_widget/pages/withdraw_page/withdraw_page.dart';
import 'package:zhiying_base_widget/widgets/custom/banner/custom_banner_widget.dart';
import 'package:zhiying_base_widget/widgets/custom/goods/custom_goods_creater.dart';
import 'package:zhiying_base_widget/widgets/custom/multi_nav/custom_quick_entry.dart';
import 'package:zhiying_base_widget/widgets/custom/notice/custom_notice_widget.dart';
import 'package:zhiying_base_widget/widgets/custom/search/custom_search_widget.dart';
import 'package:zhiying_base_widget/widgets/custom/slide_banner/custom_slide_banner_creater.dart';
import 'package:zhiying_base_widget/widgets/goods_details/footer/goods_details_footer_widget.dart';
import 'package:zhiying_base_widget/widgets/goods_details/price/goods_details_price_widget.dart';
import 'package:zhiying_base_widget/widgets/goods_details/slide_banner/goods_details_slide_banner_widget.dart';
import 'package:zhiying_base_widget/widgets/goods_details/store/store_widget.dart';
import 'package:zhiying_base_widget/widgets/goods_details/upgrade_tip/upgrade_tip_widget.dart';
import 'package:zhiying_base_widget/widgets/home/home_banner/home_banner_creater.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_creater.dart';
import 'package:zhiying_base_widget/widgets/home/home_slide_banner/home_slide_banner_creater.dart';
import 'package:zhiying_base_widget/widgets/home/home_sreach/home_sreach_creater.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_appbar/hot_ranking_appbar.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_list/hot_ranking_list.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_tab_bar/hot_tab_bar.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_data/mine_data.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_header/mine_header.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_nav/mine_nav_bg.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_nav/mine_nav_creater.dart';
import 'package:zhiying_base_widget/widgets/mine/mine_quick_entry/mine_quick_entry.dart';
import 'package:zhiying_base_widget/widgets/others/normal_nav/normal_nav_creater.dart';
import 'package:zhiying_base_widget/widgets/search/tabbar/search_tab_creater.dart';
import 'package:zhiying_base_widget/widgets/search_result/sort/search_result_sort_widget.dart';
import 'package:zhiying_base_widget/widgets/search_result/tarbar/search_result_tab_creater.dart';
import 'package:zhiying_base_widget/widgets/wallet/wallet_bil/wallet_bil.dart';
import 'package:zhiying_base_widget/widgets/wallet/wallet_data/wallet_data.dart';
import 'package:zhiying_base_widget/widgets/wallet/wallet_detail/wallet_detail.dart';
import 'package:zhiying_base_widget/widgets/wallet/wallet_income/wallet_income.dart';
import 'package:zhiying_base_widget/widgets/wallet_bil_detail/wallet_bil_detail.dart';
import 'package:zhiying_comm/util/defalut_widget_creater.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

import 'pages/custom_page/custom_page.dart';
import 'pages/favorites_page/favorites_page.dart';
import 'pages/message_notice_page/message_notice_page.dart';
import 'pages/privacy_settings_page/privacy_settings_page.dart';
import 'pages/search_page/search_page.dart';
import 'pages/wallet_page/wallet_page.dart';
import 'widgets/goods_details/coupon/counpon_widget.dart';
import 'widgets/goods_details/detail_img/goods_details_img.dart';
import 'widgets/goods_details/evaluate/goods_details_evaluate_widget.dart';
import 'widgets/goods_details/recommend/goods_detail_commend_creater.dart';
import 'widgets/goods_details/title/goods_details_title_widget.dart';
import 'widgets/home/home_auth/home_auth.dart';
import 'widgets/home/home_sreach/home_sreach_creater.dart';
import 'widgets/search/history_tag/search_history_tag.dart';
import 'widgets/search/hot_tag/search_hot_tag_widget.dart';
import 'widgets/search/input/search_input_widget.dart';
import 'widgets/search/tabbar/search_tab_creater.dart';
import 'widgets/search_result/goods_list/search_result_goods_list_creater.dart';
import 'widgets/search_result/search_input/search_result_input.dart';
import 'package:zhiying_comm/util/mob_util/mob_util.dart';

class BaseWidgetRegister {
  /// 初始化方法
  static void init() {
    BaseWidgetRegister.registPage();
    BaseWidgetRegister.registWidgets();
    BaseWidgetRegister.registInit();
  }

  static void registInit() {
    Application.addMethod(() async {
      if (await BaseSettingModel.init() == null) {
        throw Future.error('BaseSettingModel 初始化失败');
      }
    });

    Application.addMethod(() async {
      AppConfigModel config = await AppConfigModel.init();
      if (config == null) {
        Logger.debug('app 初始化失败');
        return;
      }
      ShareSDKRegister register = ShareSDKRegister();
      // ================ Weixin
      register.setupWechat(config.keys?.weixin?.appId ?? '', config.keys?.weixin?.secret ?? '', config.keys?.weixin?.universalLink ?? '');

      // ================ Weibo
      register.setupSinaWeibo(config.keys?.weibo?.appkey ?? '', config.keys?.weibo?.secret ?? '', config.keys?.weibo?.redirectUrl ?? '');

      // ================ QQ
      register.setupQQ(config.keys?.qq?.appId ?? '', config.keys?.qq?.appkey ?? '');

      // ================ jd
      if (Platform.isIOS) {
        Jdsdk.init(appKey: config.keys?.jdIos?.appkey ?? '', appSecret: config.keys?.jdIos?.secret ?? '');
      } else if (Platform.isAndroid) {
        Jdsdk.init(appKey: config.keys?.jdAndroid?.appkey ?? '', appSecret: config.keys?.jdAndroid?.secret ?? '');
      }
      SharesdkPlugin.regist(register);
    });

    // MOB 秒验
    MobUtil.init();

    // 缓存可能用到的数据，预加载
    Application.addMethod(() {
      // 精选的数据
      NetUtil.post('/api/v1/rec/featured?page=1', method: NetMethod.GET, cache: true, showToast: false);
      // 淘宝的数据
      NetUtil.post('/api/v1/rec/taobao?page=1', method: NetMethod.GET, cache: true, showToast: false);
      // 京东
      NetUtil.post('/api/v1/rec/jd?page=1', method: NetMethod.GET, cache: true, showToast: false);
      // 唯品会
      NetUtil.post('/api/v1/rec/vip?page=1', method: NetMethod.GET, cache: true, showToast: false);
      // 拼多多
      NetUtil.post('/api/v1/rec/pdd?page=1', method: NetMethod.GET, cache: true, showToast: false);
      // 苏宁
      NetUtil.post('/api/v1/rec/suning?page=1', method: NetMethod.GET, cache: true, showToast: false);
      // 考拉
      NetUtil.post('/api/v1/rec/kaola?page=1', method: NetMethod.GET, cache: true, showToast: false);
      return null;
    });

    // 延迟一秒
    Application.addMethod(() async {
      return Future.delayed(Duration(seconds: 1));
    });
  }

  // 注册页面
  static void registPage() {
    /// 闪图页
    PageFactory.regist('LaunchPage', (model) => LaunchPage());
    // PageFactory.regist('homePage', (model) => HomePage());
    // PageFactory.regist('pub.flutter.index', (model) => MainPage(model));
    /// 首页 -> 第一页
    PageFactory.regist('pub.flutter.index', (model) => CustomPage(model));

    /// 我的页面
    PageFactory.regist('pub.flutter.profile', (model) => MainPage(model));

    /// 热榜页
    PageFactory.regist('pub.flutter.hot_rank', (model) => HotRankingPage(model));

    /// 钱包页
    PageFactory.regist('pub.flutter.my_wallet', (model) => WalletPage(data: model));

    /// 商品详情页
    PageFactory.regist('goods_details', (model) => GoodsDetailsPage(model));

    /// 搜索页
    PageFactory.regist('pub.flutter.search_index', (model) => SearchPage(model));

    /// 搜索结果页
    PageFactory.regist('pub.flutter.search_result', (model) => SearchResultPage(model));

    /// 反馈页
    PageFactory.regist('pub.flutter.feedback', (model) => FeedbackPage(model));

    /// 反馈列表页
    PageFactory.regist('pub.flutter.feedback_list', (model) => FeedbackRecordPage(model));

    /// 微信导师
    PageFactory.regist('pub.flutter.wechat_teacher', (model) => WechatTeacherPage(model));

    /// 提现页
    PageFactory.regist('pub.flutter.cash_out', (model) => WithdrawPage(model));

    /// webview
    PageFactory.regist('pub.flutter.url', (model) => BaseWebview(model));

    PageFactory.regist('pub.flutter.profile_settings', (model) => MineDetailPage());
    PageFactory.regist('pub.flutter.settings', (model) => SettingPage(model));

    /// 订单页面
    PageFactory.regist('pub.flutter.my_order', (model) => OrdersPage(model));
    PageFactory.regist('pub.flutter.account_security', (model) => SecurityPage(model));
    PageFactory.regist('pub.flutter.account_security_alipay', (model) => SecurityBindAlipayPage(model));

    /// 登录密码
    PageFactory.regist('pub.flutter.account_security_password', (model) => SecurityPassword(model));

    /// 修改手机号
    PageFactory.regist('pub.flutter.account_security_mobile', (model) => SecurityMobile(model));

    /// 邀请好友
    PageFactory.regist('pub.flutter.invite_friends', (model) => InvitedFriendsPage(model));

    /// 我的团队
    PageFactory.regist('pub.flutter.my_team', (model) => TeamPage(model));

    /// 用户详情
    PageFactory.regist('pub.flutter.my_fan_detail', (model) => TeamDetailsPage(model));

    /// 消息中心
    PageFactory.regist('pub.flutter.message_center', (model) => MessageNoticePage(model));

    /// TODO 首页的消息中心标识和我的页面不一致，需要改
    PageFactory.regist('pub.flutter.message', (model) => MessageNoticePage(model));

    /// 我的收藏
    PageFactory.regist('pub.flutter.my_fav', (model) => FavoritesPage(model));

    /// 关于我们
    PageFactory.regist('pub.flutter.about_us', (model) => AboutUsPage(model));

    /// 隐私设置
    PageFactory.regist('pub.flutter.privacy_settings', (model) => PrivacySettingsPage(model));

    /// 注销账号页
    PageFactory.regist('pub.flutter.account_delete', (model) => AccountDeletePage(model));

    /// 消息设置
    PageFactory.regist('pub.flutter.message_settings', (model) => MessageSettingsPage(model));

    /// 钱包明细
    PageFactory.regist('pub.flutter.my_wallet_detail', (model) => BilDetailPage(model));

    /// 通用模块
    PageFactory.regist('pub.flutter.custom', (model) => CustomPage(model));
  }

  // 注册控件
  static void registWidgets() {
    // ==================== 通用组件
    // 导航栏
    WidgetFactory.regist('normal_nav', NormalNavCreater());

    // ==================== 首页
    // WidgetFactory.regist('index_title', NormalNavCreater());
    /// 首页搜索栏
    WidgetFactory.regist('index_search', HomeSreachCreater());
    // WidgetFactory.regist('index_search',
    //     DefaultWidgetCreater((model) => HomeSreachWidget(model)));

    /// 可滚动banner
    WidgetFactory.regist('index_carousel', HomeSlideBannerCreater());

    /// 首页商品推荐列表
    WidgetFactory.regist('index_recommend_list', GoodsListCreater());

    /// 首页快速入口
    // WidgetFactory.regist('multi_nav', DefaultWidgetCreater((model) => HomeQuickEntry(model)));

    /// 滚动公告
    WidgetFactory.regist('index_placard', DefaultWidgetCreater((model) => CustomNoticeWidget(model)));

    /// 不可以滚动banner
    WidgetFactory.regist('index_banner_one', HomeBannerCreater());
    WidgetFactory.regist('index_banner_two', HomeBannerCreater());
    // WidgetFactory.regist('index_taobao_auth_tip', HomeAuthCreater());

    /// ==================== 搜索页面 ==================== ///
    // 搜索输入框
    WidgetFactory.regist('search_index_input', DefaultWidgetCreater((model) => SearchInputWidget(model)));
    // // 搜索tabBar
    WidgetFactory.regist('search_index_icon_list', SearcchTabCreater());
    // // 热门搜索标签
    WidgetFactory.regist('search_index_hot_keyword', DefaultWidgetCreater((model) => SearchHotTagWidget(model)));
    // // 历史搜索标签
    WidgetFactory.regist('search_index_history', DefaultWidgetCreater((model) => SearchHistoryTagWidget(model)));

    /// ==================== 搜索结果页面 ==================== ///
    // 输入框
    WidgetFactory.regist('search_result_input', DefaultWidgetCreater((model) => SearchResultInputWidget(model)));
    // tabbar
    WidgetFactory.regist('search_result_icon_list', SearchResultTabCreater());
    // 搜索筛选Widget
    WidgetFactory.regist('search_conditions', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));

    // 搜索结果页筛选widget 淘宝
    WidgetFactory.regist('search_result_taobao_sort', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));
    // 搜索结果的商品列表   淘宝
    WidgetFactory.regist('search_result_taobao_item', SearchResultGoodsListCreater());

    // 搜索结果页筛选widget 京东
    WidgetFactory.regist('search_result_jd_sort', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));
    // 搜索结果的商品列表  京东
    WidgetFactory.regist('search_result_jd_item', SearchResultGoodsListCreater());

    // 搜索结果页筛选widget 苏宁
    WidgetFactory.regist('search_result_suning_sort', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));
    // 搜索结果的商品列表  苏宁
    WidgetFactory.regist('search_result_suning_item', SearchResultGoodsListCreater());

    // 搜索结果页筛选widget 拼多多
    WidgetFactory.regist('search_result_pdd_sort', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));
    // 搜索结果的商品列表  拼多多
    WidgetFactory.regist('search_result_pdd_item', SearchResultGoodsListCreater());

    // 搜索结果页筛选widget 唯品会
    WidgetFactory.regist('search_result_vip_sort', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));
    // 搜索结果的商品列表  唯品会
    WidgetFactory.regist('search_result_vip_item', SearchResultGoodsListCreater());

    // 搜索结果页筛选widget 考拉
    WidgetFactory.regist('search_result_kaola_sort', DefaultWidgetCreater((model) => SearchResultSortWidget(model)));
    // 搜索结果的商品列表 考拉
    WidgetFactory.regist('search_result_kaola_item', SearchResultGoodsListCreater());

    /// ==================== 商品详情 ==================== ///
    // 商品详情轮播图
    WidgetFactory.regist('product_detail_carousel', DefaultWidgetCreater((model) => GoodsDetailsSlideBannerWidget(model)));
    // 商品详情下载APP提示
    WidgetFactory.regist('product_detail_download_tips', DefaultWidgetCreater((model) => UpgradeTipWidget(model)));
    // 商品详情价格显示
    WidgetFactory.regist('product_detail_price', DefaultWidgetCreater((model) => GoodsDetailsPriceWidget(model)));
    // 商品详情标题
    WidgetFactory.regist('product_detail_title', DefaultWidgetCreater((model) => GoodsDetailsTitleWidget(model)));
    // 商品详情优惠劵
    WidgetFactory.regist('product_detail_coupon', DefaultWidgetCreater((model) => CounponWidget(model)));
    // 商品详情店铺
    WidgetFactory.regist('product_detail_shop', DefaultWidgetCreater((model) => StoreWidget(model)));
    // 商品详情宝贝评价
    WidgetFactory.regist('product_detail_comment', DefaultWidgetCreater((model) => GoodsDetailsEvaluateWidget(model)));
    // 商品详情图片
    WidgetFactory.regist('product_detail_img_list', DefaultWidgetCreater((model) => GoodsDetailsImgWidget(model)));
    // 商品详情底部推荐列表
    WidgetFactory.regist('product_detail_bottom_rec', GoodsDetailCommendCreater());
    // 商品详情底部
    WidgetFactory.regist('product_detail_bottom', DefaultWidgetCreater((model) => GoodsDetailsFooterWidget(model)));

    // ==================== 个人中心
    WidgetFactory.regist('profile_appbar', MineNavCreater());
    WidgetFactory.regist('profile_background', DefaultWidgetCreater((model) => MineNavBg(model)));
    WidgetFactory.regist('profile_header', DefaultWidgetCreater((model) => MineHeader(model)));
    WidgetFactory.regist('profile_earning', DefaultWidgetCreater((model) => MineData(model)));
    WidgetFactory.regist('profile_functions', DefaultWidgetCreater((model) => MineQuickEntry(model)));
    WidgetFactory.regist('profile_my_functions', DefaultWidgetCreater((model) => MineQuickEntry(model)));
    // WidgetFactory.regist('profile_carousel', DefaultWidgetCreater((model) => HomeBannerWidget(model)));
    // WidgetFactory.regist('profile_carousel', HomeBannerCreater());

    // ==================== 钱包
    // WidgetFactory.regist(
    //     'my_wallet_appbar',
    //     DefaultWidgetCreater((model) => WalletAppbar(
    //           data: model,
    //         )));
    WidgetFactory.regist('my_wallet_appbar', NormalNavCreater());

    WidgetFactory.regist('my_wallet_header', DefaultWidgetCreater((model) => WalletData(model)));
    WidgetFactory.regist('my_wallet_bil', DefaultWidgetCreater((model) => WalletBil(model)));
//    WidgetFactory.regist(
//        'wallet_detail', DefaultWidgetCreater((model) => WalletDetail()));
//     WidgetFactory.regist('wallet_detail', HomeAuthCreater());
    WidgetFactory.regist('my_wallet_providers', DefaultWidgetCreater((model) => WalletDetail(model)));

    WidgetFactory.regist('wallet_income', DefaultWidgetCreater((model) => WalletIncome()));

    //======================= 账单明细
    WidgetFactory.regist(
        'my_wallet_detail',
        DefaultWidgetCreater((model) => WalletBilDetail(
              data: model,
            )));

    //======================== 热榜
    WidgetFactory.regist('hot_rank_appbar', DefaultWidgetCreater((model) => HotRankingAppBar(model)));
    WidgetFactory.regist('hot_rank_tabs', DefaultWidgetCreater((model) => HotRankTableBar(model)));
    WidgetFactory.regist('hot_rank_tab_view', DefaultWidgetCreater((model) => HotRankingList(model)));

    /// ==================== 通用模块 ==================== ///
    // 搜索
    WidgetFactory.regist('search', DefaultWidgetCreater((model) => CustomSearchWidget(model)));
    // 轮播广告位, 可滑动
    WidgetFactory.regist('carousel', CustomSlideBannerCreater());
    // 多眼导航，可滑动
    WidgetFactory.regist('multi_nav', DefaultWidgetCreater((model) => CustomQuickEntry(model)));
    // banner, 不可滑动
    WidgetFactory.regist('banner', DefaultWidgetCreater((model) => CustomBannerWidget(model)));
    // 商品列表
    WidgetFactory.regist('product', CustomGoodsCreater());
    // 公告头条
    WidgetFactory.regist('placard', DefaultWidgetCreater((model) => CustomNoticeWidget(model)));
    // 授权
    WidgetFactory.regist('audit_tip', DefaultWidgetCreater((model) => HomeAuth(model)));
  }
}
