// ignore_for_file: must_be_immutable

import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/scroll/refreshIndicator.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/patient_card.dart';
import 'package:get/get.dart';

import 'widgets/main_content_view.dart';

class ConsultationPage extends FisView<ConsultationController>
    implements FPage {
  @override
  String pageName;

  ConsultationPage({this.pageName = 'ConsultationPage', Key? key})
      : super(key: key);

  @override
  FWidget build(BuildContext context) {
    controller.businessParent = this;
    return context.fResponsiveVoid(
      mobile: () => _MobileLayout(this),
      desktop: () => _DesktopLayout(this),
    );
  }
}

class _MobileLayoutTab extends FisView<ConsultationController>
    implements FWidget {
  bool get isRefer => controller.state.patientTab == 1;

  ///父级节点
  final FPage businessParent;
  _MobileLayoutTab(this.businessParent);
  @override
  FWidget build(BuildContext context) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        FTextButton(
          name: "myPatient",
          businessParent: businessParent,
          onPressed: () {
            controller.state.patientTab = 0;
            controller.onLoad();
          },
          child: FObx(
            () => FText(
              i18nBook.remedical.myPatient.t,
              style: TextStyle(
                fontSize: 16,
                color: isRefer
                    ? FTheme.ins.colorScheme.text
                    : FTheme.ins.colorScheme.primary,
              ),
            ),
          ),
        ),
        FTextButton(
          name: "referPatient",
          businessParent: businessParent,
          onPressed: () {
            controller.state.patientTab = 1;
            controller.onLoad();
          },
          child: FObx(
            () => FText(
              i18nBook.remedical.referPatient.t,
              style: TextStyle(
                fontSize: 16,
                color: isRefer
                    ? FTheme.ins.colorScheme.primary
                    : FTheme.ins.colorScheme.text,
              ),
            ),
          ),
        ),
      ],
    );
  }
}

class _MobileLayout extends FisView<ConsultationController> implements FWidget {
  final ScrollController scrollController = ScrollController();

  ///父级节点
  final FPage businessParent;
  _MobileLayout(this.businessParent);
  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      body: FSafeArea(
        child: FColumn(
          // mainAxisSize: MainAxisSize.max,
          children: [
            FContainer(
              decoration: BoxDecoration(
                  //TODO:[Gavin] 等全局状态栏设计完善后再修改
                  // color: Colors.white,
                  ),
              child: FColumn(
                children: [
                  _MobileLayoutTab(businessParent),
                  FContainer(
                    margin: EdgeInsets.only(
                      left: 10,
                      right: 10,
                      bottom: 10,
                    ),
                    child: FRow(
                      children: [
                        FBorderInput(
                          focusNode: controller.searchFocusNode,
                          controller: controller.serachInput,
                          fillColor: Colors.white,
                          hintSize: 14,
                          contentSize: 14,
                          maxLength: 50,
                          borderColor: Color.fromARGB(255, 185, 184, 184),
                          suffixIcon: FIconButton(
                            name: "search",
                            onPressed: () => controller.patientInfoController
                                .getPatients(true),
                            icon: FIcon(
                              Icons.search,
                              size: 16,
                            ),
                            businessParent: this.businessParent,
                          ),
                          height: 40,
                          hintText: i18nBook.remedical.searchPatient.t,
                          onChanged: (value) {
                            controller.state.keyWord = value;
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            FExpanded(
              child: _MobileLayoutRefreshIndicator(this.businessParent),
            )
          ],
        ),
      ),
    );
  }
}

class _MobileLayoutRefreshIndicator extends StatelessWidget implements FWidget {
  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
      new GlobalKey<RefreshIndicatorState>();
  final controller = Get.find<ConsultationController>();

  ///父级节点
  final FPage businessParent;

  _MobileLayoutRefreshIndicator(this.businessParent);
  Future<void> _getData() async {
    controller.onLoad();
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      child: FRefreshIndicator(
        key: _refreshIndicatorKey,
        onRefresh: _getData,
        child: FObx(
          () => FListView.builder(
            shrinkWrap: true,
            physics: const AlwaysScrollableScrollPhysics(),
            itemCount: controller.state.patientInfos.length,
            controller: controller.patientCardScrollController,
            itemBuilder: (context, index) {
              /// 是否是已加载的最后一个
              final isLoadedLast =
                  index == controller.state.patientInfos.length - 1;
              final isLast = index == controller.state.listTotalCount - 1;
              final patientInfo = controller.state.patientInfos[index];
              final card = FGestureDetector(
                name: "editPatient${patientInfo.name}",
                businessParent: businessParent,
                onTap: () {
                  controller.patientInfoController.editPatient(patientInfo);
                },
                child: PatientCard(
                  index,
                  patientInfo,
                ),
              );
              if (!isLoadedLast) {
                return card;
              } else {
                return FColumn(children: [
                  card,
                  FContainer(
                    height: 50,
                    child: FCenter(
                      child: FText(
                        isLast
                            ? i18nBook.remedical.noMore.t
                            : i18nBook.common.loading.t,
                        style: TextStyle(
                          fontSize: 14,
                          color: Colors.grey,
                        ),
                      ),
                    ),
                  )
                ]);
              }
            },
          ),
        ),
      ),
    );
  }
}

class _DesktopLayout extends FisView<ConsultationController>
    implements FWidget {
  final ScrollController scrollController = ScrollController();

  final FPage businessParent;

  _DesktopLayout(this.businessParent);

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      color: Colors.grey.shade200,
      child: FRow(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FExpanded(
            child: FContainer(
              child: MainContentView(this.businessParent),
            ),
          ),
        ],
      ),
    );
  }
}
