import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/constants/bg_color.dart';
import 'package:sk_base_mobile/models/base_search_more.model.dart';
import 'package:sk_base_mobile/models/base_search_more_controller.dart';
import 'package:sk_base_mobile/util/debouncer.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:sk_base_mobile/widgets/empty.dart';
import 'package:sk_base_mobile/widgets/gradient_button.dart';

class SkMutilSearchMore<T extends BaseSearchMoreModel> extends StatelessWidget {
  final Function(List<int>)? onOk;
  final Function(T)? beforeSelectedCheck;
  // ignore: prefer_typing_uninitialized_variables
  final BaseSearchMoreController controller;
  final bool enablePullUp;
  final bool enablePullDown;
  final bool isDialog;

  final Function(int)? leadingBuilder;
  SkMutilSearchMore(
      {super.key,
      required this.controller,
      this.onOk,
      this.isDialog = true,
      this.beforeSelectedCheck,
      this.leadingBuilder,
      this.enablePullDown = true,
      this.enablePullUp = true});
  final listTitleTextStyle =
      TextStyle(fontSize: ScreenAdaper.height(20), fontWeight: FontWeight.w600);
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(
            horizontal: ScreenAdaper.width(20),
            vertical: ScreenAdaper.height(20)),
        child: Column(
          children: [
            buildDialogHeader(),
            SizedBox(
              height: ScreenAdaper.height(defaultPadding) / 2,
            ),
            buildSearchBar(),
            SizedBox(
              height: ScreenAdaper.height(defaultPadding) / 2,
            ),
            Expanded(child: buildList()),
            SizedBox(
              height: ScreenAdaper.height(defaultPadding) / 2,
            ),
            GradientButton(
              onPressed: () {
                if (onOk != null) {
                  onOk!(controller.selectedIndex);
                }
                if (isDialog) {
                  Get.back();
                }
                controller.selectedIndex.clear();
              },
              buttonText: '确定',
            )
          ],
        ));
  }

  Widget buildDialogHeader() {
    return Container(
      height: ScreenAdaper.height(60),
      child: Stack(
        children: [
          Center(
            child: Text(
              '请选择',
              style: TextStyle(
                  fontSize: ScreenAdaper.height(30),
                  fontWeight: FontWeight.w600),
            ),
          ),
          Positioned(
              right: 0,
              child: GestureDetector(
                  onTap: () {
                    Get.back();
                  },
                  child: Icon(
                    Icons.close,
                    size: ScreenAdaper.height(40),
                  )))
        ],
      ),
    );
  }

  Widget buildSearchBar() {
    final doSearch = debouncer((String value) {
      controller.searchKey.value = value;
      controller.onRefresh();
    }, delayTime: 500);

    return Row(
      children: [
        Expanded(
            flex: 5,
            child: TextField(
                controller: controller.searchBarTextConroller,
                onChanged: (value) => doSearch(value),
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.symmetric(
                      vertical: ScreenAdaper.height(10),
                      horizontal: ScreenAdaper.width(10)),
                  hintText: '名称',
                  floatingLabelBehavior: FloatingLabelBehavior.always,
                  prefixIcon: const Icon(Icons.search),
                  // 当searchBarController有值时不显示
                  suffixIcon: Obx(() => controller.searchKey.value.isEmpty
                      ? const SizedBox()
                      : IconButton(
                          icon: const Icon(Icons.clear),
                          onPressed: () {
                            controller.searchKey.value = '';
                            controller.searchBarTextConroller.clear();
                            doSearch('');
                          },
                        )),
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ))),
      ],
    );
  }

  Widget buildList() {
    return Obx(() => SmartRefresher(
        enablePullDown: enablePullDown,
        enablePullUp: enablePullUp,
        controller: controller.refreshController,
        onLoading: controller.onLoading,
        onRefresh: controller.onRefresh,
        child: controller.list.isEmpty
            ? const Center(
                child: Empty(text: '暂无数据'),
              )
            : ListView.separated(
                separatorBuilder: (context, index) => const Divider(
                      color: AppTheme.dividerColor,
                      height: 1,
                    ),
                itemCount: controller.list.length,
                itemBuilder: (_, index) => buildItem(index))));
  }

  Widget buildItem(int index) {
    return InkWell(
      onTap: () {
        controller.selectedIndex.contains(index)
            ? controller.selectedIndex.remove(index)
            : controller.selectedIndex.add(index);
      },
      child: Container(
        padding: EdgeInsets.symmetric(
            horizontal: ScreenAdaper.width(5),
            vertical: ScreenAdaper.height(20)),
        child: Row(
          children: [
            Expanded(
              child: leadingBuilder != null
                  ? leadingBuilder!(index)
                  : Text(
                      '-',
                      style: TextStyle(fontSize: ScreenAdaper.height(30)),
                    ),
            ),
            Obx(() => controller.selectedIndex.contains(index)
                ? Icon(
                    Icons.check_circle,
                    color: Colors.green,
                    size: ScreenAdaper.height(40),
                  )
                : const SizedBox())
          ],
        ),
      ),
    );
  }
}
