import 'package:fis_ui/base_define/page.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/pages/remedical/vocabulary_entry/widgets/entries_item.dart';
import 'package:flyinsono/pages/user/report_template/widgets/add_template_page.dart';
import 'package:flyinsono/pages/user/report_template/widgets/template_simple_card.dart';

import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

import '../widgets/search_input.dart';
import 'index.dart';

///报告模板页面
class ReportTemplatePage extends FisView<ReportTemplateController>
    implements FPage {
  const ReportTemplatePage({Key? key}) : super(key: key);
  @override
  final String pageName = 'ReportTemplatePage';
  @override
  FWidget build(BuildContext context) {
    return context.fResponsiveVoid(
      mobile: () => _MobileLayout(this),
      desktop: () => _DesktopLayout(this),
    );
  }
}

class _DesktopLayout extends FisView<ReportTemplateController>
    implements FWidget {
  ///父级节点
  final FPage businessParent;
  _DesktopLayout(this.businessParent);
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      color: FTheme.ins.colorScheme.background,
      child: FRow(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FContainer(
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            width: 400,
            child: FColumn(
              children: [
                SearchInput(
                  businessParent: businessParent,
                  onChanged: (value) {
                    controller.state.key = value;
                  },
                  serchOnPressed: () {
                    controller.search();
                  },
                  hintText: i18nBook.user.templateSearch.t,
                ),
                FContainer(
                  padding: EdgeInsets.symmetric(
                    horizontal: 20,
                  ),
                  child: _buildOperationLine(context),
                ),
                const FSizedBox(height: 7),
                _BuildDivider(),
                const FSizedBox(
                  height: 10,
                ),
                _BuildTemplates(),
              ],
            ),
          ),
          const FSizedBox(width: 10),
          FObx(() {
            if (controller.state.isInitTemplates) {
              return _BuildTemplateContentView();
            } else {
              return FExpanded(
                  child: FContainer(
                alignment: Alignment.center,
                child: NoDataWidget(),
              ));
            }
          }),
        ],
      ),
    );
  }

  FWidget _buildTextButton(IconData icons, String text, Function onTap) {
    return FGestureDetector(
      name: text,
      businessParent: businessParent,
      onTap: () async {
        onTap.call();
      },
      child: FMouseRegion(
        cursor: SystemMouseCursors.click,
        child: FCenter(
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FIcon(
                icons,
                size: 24,
                color: FTheme.ins.colorScheme.primary,
              ),
              FText(
                text,
                style: TextStyle(
                  height: 1,
                  fontSize: 16,
                  color: Colors.black,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///操作行，添加、刷新
  FWidget _buildOperationLine(BuildContext context) {
    return FContainer(
      margin: EdgeInsets.symmetric(horizontal: 10),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildTextButton(
            Icons.add_box_outlined,
            i18nBook.user.addTemplate.t,
            () async {
              await controller.getPublishTemplates();
              router.dialog(
                () => AddTemplatePage(),
                dismissible: true,
              );
            },
          ),
          _buildTextButton(
            Icons.refresh,
            i18nBook.common.refresh.t,
            () async {
              await controller.refershTemplates();
            },
          ),
        ],
      ),
    );
  }
}

///横向分割线
class _BuildDivider extends FisView<ReportTemplateController>
    implements FWidget {
  @override
  FWidget build(BuildContext context) {
    return FPadding(
      padding: EdgeInsets.symmetric(
        horizontal: 20,
      ),
      child: const FDivider(height: 1, color: Colors.grey),
    );
  }
}

class _MobileLayout extends FisView<ReportTemplateController>
    implements FWidget {
  ///父级节点
  final FPage businessParent;
  _MobileLayout(this.businessParent);
  @override
  FWidget build(BuildContext context) {
    throw UnimplementedError();
  }
}

///模板内容预览
class _BuildTemplateContentView extends FisView<ReportTemplateController> {
  final ScrollController _scrollController = ScrollController();
  @override
  FWidget build(BuildContext context) {
    var jpgUrls = controller.state.selectedTemplate.templatePreviewUrlList;
    var jpgMode = jpgUrls!.length > 0;
    double horizontalPadding = MediaQuery.of(context).size.width > 1400
        ? (MediaQuery.of(context).size.width - 1400) / 2
        : 0;
    return FExpanded(
      child: FContainer(
        alignment: Alignment.center,
        color: Colors.grey[200],
        child: FAlwaysScrollListView(
          scrollController: _scrollController,
          child: QuickFWidget(ScrollConfiguration(
            behavior:
                ScrollConfiguration.of(context).copyWith(scrollbars: false),
            child: FListView(
              controller: _scrollController,
              children: [
                ...jpgUrls.map((e) {
                  return FContainer(
                    padding: EdgeInsets.symmetric(
                      horizontal: horizontalPadding,
                      vertical: 30,
                    ),
                    margin: EdgeInsets.only(bottom: 5),
                    child: FImage.network(
                      e,
                      fit: BoxFit.contain,
                    ),
                  );
                }),
              ],
            ),
          )),
        ),
      ),
    );
  }
}

///报告模板列表
class _BuildTemplates extends FisView<ReportTemplateController> {
  final ScrollController _scrollController = ScrollController();
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: MediaQuery.of(context).size.height - 180,
      child: FObx(
        () => controller.state.templates.length == 0
            ? NoDataWidget()
            : FAlwaysScrollListView(
                scrollController: _scrollController,
                child: QuickFWidget(ScrollConfiguration(
                  behavior: ScrollConfiguration.of(context)
                      .copyWith(scrollbars: false),
                  child: FListView.builder(
                    shrinkWrap: true,
                    itemCount: controller.state.templates.length,
                    controller: _scrollController,
                    itemBuilder: (context, index) {
                      return FObx(() => SwipeCard(
                            index: index,
                            isActive:
                                controller.state.selectTemplateIndex == index,
                            onTap: () async {
                              await controller.onSelectedTemplate(index);
                            },
                            child: TemplateSimpleCard(index),
                          ));
                    },
                  ),
                )),
              ),
      ),
    );
  }
}
