import 'package:cs_resources/generated/assets.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:router/ext/auto_router_extensions.dart';
import 'package:shared/utils/color_utils.dart';
import 'package:widgets/ext/ex_widget.dart';
import 'package:widgets/load_state_layout.dart';
import 'package:widgets/my_button.dart';
import 'package:widgets/my_load_image.dart';
import 'package:widgets/my_text_view.dart';
import 'package:widgets/utils/dark_theme_util.dart';

import '../../../router/page/property_page_router.dart';
import 'property_ioan_vm.dart';

@RoutePage()
class PropertyIoanPage extends HookConsumerWidget {
  const PropertyIoanPage({Key? key}) : super(key: key);

  //启动当前页面
  static void startInstance({BuildContext? context}) {
    if (context != null) {
      context.router.push(const PropertyIoanPageRoute());
    } else {
      appRouter.push(const PropertyIoanPageRoute());
    }
  }

  Widget _buildContentTopAdviceSection(BuildContext context,WidgetRef ref, _vm){
    return Column(
      children: [
        MyTextView(
          "24IFM",
          textColor: DarkThemeUtil.multiColors(context, context.appColors.textBlack, darkColor: Colors.white),
          fontSize: 27,
          isFontMedium: true,
          textAlign: TextAlign.center,
        ),
        MyTextView(
          S.current.find_best_loan,
          textColor: DarkThemeUtil.multiColors(context, context.appColors.textBlack, darkColor: Colors.white),
          fontSize: 16,
          isFontRegular: true,
          marginBottom: 14,
          textAlign: TextAlign.center,
        ),
        Container(
          width: double.infinity,
          height: 210,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(0),
            color: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#FEFEFE'), darkColor: Colors.black),
            image: const DecorationImage(
              image: AssetImage('packages/cs_resources/${Assets.propertyHomeLoanBg}'), // 使用包中的资源
              fit: BoxFit.cover,
            ),
          ),
          child: Column(
            children: [
              MyTextView(
                S.current.exclusive_rates,
                textColor: DarkThemeUtil.multiColors(context, ColorUtils.string2Color('#FEFEFE'), darkColor: Colors.white),
                fontSize: 15,
                boxWidth: 237,
                boxHeight: 35,
                alignment: Alignment.center,
                textAlign: TextAlign.center,
                marginTop: 5.5,
                backgroundColor: DarkThemeUtil.multiColors(context, context.appColors.textPrimary, darkColor: Colors.black),
              ),
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                        width: 105,
                        height: 105,
                        padding: const EdgeInsets.all(5),
                        color: DarkThemeUtil.multiColors(context, context.appColors.textPrimary, darkColor: Colors.black),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            MyTextView(
                              _vm.state.lowestFloatingRate,
                              textColor: ColorUtils.string2Color('#FEFEFE'),
                              fontSize: 30,
                            ),
                            MyTextView(
                              S.current.lowest_floating_rate,
                              textColor: ColorUtils.string2Color('#FEFEFE'),
                              fontSize: 14,
                              textAlign: TextAlign.center,
                            ),
                          ]
                        )
                    ),
                    Container(
                        width: 105,
                        height: 105,
                        padding: const EdgeInsets.all(5),
                        color: DarkThemeUtil.multiColors(context, context.appColors.textPrimary, darkColor: Colors.black),
                        margin: const EdgeInsets.only(left: 18),
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              MyTextView(
                                _vm.state.lowestFixedRate,
                                textColor: ColorUtils.string2Color('#FEFEFE'),
                                fontSize: 30,
                              ),
                              MyTextView(
                                S.current.lowest_floating_rate,
                                textColor: ColorUtils.string2Color('#FEFEFE'),
                                fontSize: 14,
                                textAlign: TextAlign.center,
                              ),
                            ]
                        )
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        MyTextView(
          S.current.other_offering,
          textColor: ColorUtils.string2Color('#FEFEFE'),
          fontSize: 15,
          boxWidth: double.infinity,
          boxHeight: 40.5,
          alignment: Alignment.center,
          textAlign: TextAlign.center,
          backgroundColor: DarkThemeUtil.multiColors(context, context.appColors.textPrimary, darkColor: Colors.black),
        ),

        const SizedBox(height: 20),

        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            MyLoadImage(
              Assets.propertyAdvicePic,
              width: 132,
              // height: 122.5,
            ),
            Expanded(
              child: Container(
                // color: Colors.red,
                padding: const EdgeInsets.only(left: 20,right: 20),
                child: Column(
                  // mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        MyTextView(
                          S.current.floating_rate,
                          textColor: context.appColors.textBlack,
                          fontSize: 14,
                          textAlign: TextAlign.center,
                        ),
                        MyTextView(
                          S.current.fixed_rate,
                          textColor: context.appColors.textBlack,
                          fontSize: 14,
                          textAlign: TextAlign.center,
                        ),
                      ],
                    ),
                    // const SizedBox(height: 10),

                    Expanded(child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            MyTextView(
                              "3.08%",
                              textColor: context.appColors.textBlack,
                              fontSize: 16,
                              textAlign: TextAlign.center,
                              isFontMedium: true,
                              marginLeft: 15,
                            ),
                            MyTextView(
                              "2.50%",
                              textColor: context.appColors.textBlack,
                              fontSize: 16,
                              textAlign: TextAlign.center,
                              isFontMedium: true,
                              marginLeft: 50.5,
                            ),
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            MyTextView(
                              "3.08%",
                              textColor: context.appColors.textBlack,
                              fontSize: 16,
                              textAlign: TextAlign.center,
                              isFontMedium: true,
                              marginLeft: 15,
                            ),
                            MyTextView(
                              "2.50%",
                              textColor: context.appColors.textBlack,
                              fontSize: 16,
                              textAlign: TextAlign.center,
                              isFontMedium: true,
                              marginLeft: 50.5,
                            ),
                          ],
                        ).marginOnly(top: 15),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            MyTextView(
                              "3.08%",
                              textColor: context.appColors.textBlack,
                              fontSize: 16,
                              textAlign: TextAlign.center,
                              isFontMedium: true,
                              marginLeft: 15,
                            ),
                            MyTextView(
                              "2.50%",
                              textColor: context.appColors.textBlack,
                              fontSize: 16,
                              textAlign: TextAlign.center,
                              isFontMedium: true,
                              marginLeft: 47.5,
                            ),
                          ],
                        ).marginOnly(top: 15),
                      ],
                    ))


                  ],
                ),
              ),
            ),
          ],
        ).constrained(height: 160),
      ],
    );
  }

  Widget _buildContentMiddleTextSection(BuildContext context,WidgetRef ref, _vm) {
    return Column(
        children: [
          MyTextView(
            S.current.choose_best_package,
            textColor: DarkThemeUtil.multiColors(context, context.appColors.textBlack, darkColor: Colors.white),
            fontSize: 18,
            isFontMedium: true,
            marginTop: 20,
            marginBottom: 20,
            textAlign: TextAlign.center,
          ),
          MyTextView(
            S.current.text_no_hidden_fees,
            textColor: DarkThemeUtil.multiColors(context, context.appColors.textBlack, darkColor: Colors.white),
            fontSize: 15,
            textAlign: TextAlign.center,
            marginBottom: 20,
          ),
        ],
    );
  }

  Widget _buildContentOfferSection(BuildContext context,WidgetRef ref, _vm) {
    final offerTextInfoList = _vm.state.offerTextInfoList.map((value) => value).toList();
    return Container(
      width: double.infinity,
      child: Column(
        children: [
          MyTextView(
           S.current.what_do_we_offer,
            textColor: DarkThemeUtil.multiColors(context, context.appColors.textBlack, darkColor: Colors.white),
            fontSize: 18,
            isFontMedium: true,
            marginTop: 16.5,
            marginBottom: 16.5,
          ),
          Wrap(
            children: List.generate(offerTextInfoList.length, (index){
              return Container(
                width: MediaQuery.of(context).size.width / 2 - 30,
                margin: const EdgeInsets.only(right: 15,bottom: 15),
                padding: const EdgeInsets.only(left: 15,right: 15,top:15,bottom:15,),
                // decoration: BoxDecoration(
                //   borderRadius: BorderRadius.circular(10),
                //   color: ColorUtils.string2Color('#F5F5F5'),
                // ),
                child: Column(
                  children: [
                    MyLoadImage(
                      offerTextInfoList[index]["icon"],
                      width: offerTextInfoList[index]["iconWidth"],
                      height: offerTextInfoList[index]["iconHeight"],
                    ),
                    const SizedBox(height: 13,),
                    MyTextView(
                      offerTextInfoList[index]["title"],
                      fontSize: 15,
                      textColor: context.appColors.textBlack,
                      textAlign: TextAlign.center,
                    ),
                  ]
                ),
              );
            }),
          ),
        ]
      ),
    );
  }

  Widget _buildContentBottomPartnersSection(BuildContext context,WidgetRef ref, _vm) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        MyTextView(
          S.current.our_partners,
          textColor: context.appColors.textBlack,
          fontSize: 18,
          isFontMedium: true,
          marginBottom: 16.5,
        ),
        MyLoadImage(Assets.propertyOurPartners,),
      ],
    );
  }

  Widget _buildCotentBox(BuildContext context,WidgetRef ref, _vm) {
    return Column(
      children: [
        _buildContentTopAdviceSection(context, ref, _vm),
        const SizedBox(height: 15,),
        _buildContentMiddleTextSection(context, ref, _vm),
        const SizedBox(height: 15,),
        _buildContentOfferSection(context, ref, _vm),
        const SizedBox(height: 15,),
        _buildContentBottomPartnersSection(context, ref, _vm),
      ],
    );
  }


  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final _vm = ref.read(propertyIoanVmProvider.notifier);
    final state = ref.watch(propertyIoanVmProvider);

    useEffect((){
      _vm.initPageData();
      return () {};
    }, []);

    return Scaffold(
      backgroundColor: DarkThemeUtil.multiColors(context, ColorUtils.string2Color("#F2F3F6"), darkColor: Colors.black),
      // appBar: AppBar(title: Text("资产")),
      body: Column(
        children: [
          Expanded(
            child: LoadStateLayout(
              state: state.loadingState,
              errorMessage: state.errorMessage,
              errorRetry: () {
                _vm.retryRequest();
              },
              successWidget: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                physics: const BouncingScrollPhysics(),
                clipBehavior: Clip.none,
                child: Padding(
                    padding: const EdgeInsets.only(left: 15,right: 15,top: 15),
                    child: _buildCotentBox(context, ref, _vm),
                )
              ),
            )
          ),
          // Visibility(
          //   visible: state.loadingState == LoadState.State_Success,
          //   child: Container(
          //     height: 50,
          //     color: context.appColors.textPrimary,
          //     child: Row(
          //       mainAxisAlignment: MainAxisAlignment.center,
          //       children: [
          //         // MyLoadImage(Assets.propertyIoanItemBg,width: 60,height: 50,),
          //         const SizedBox(width: 10,),
          //         MyTextView(
          //           "Request a Quote",
          //           fontSize: 16,
          //           textColor: context.appColors.textWhite,
          //           isFontMedium: true,
          //         ),
          //       ],
          //     ),
          //   ).onTap((){
          //     _vm.handlerRequestQuote(context);
          //   }),
          // )
        ],
      ),
    );
  }
}
