import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/lab_loading_wrapper.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flutter/material.dart';
import 'classify_list.dart';

class ClassifyDataSelector extends StatelessWidget {
  const ClassifyDataSelector({
    Key? key,
    required this.selectedClassifyIndex,
    required this.selectedItemIndex,
    required this.onItemClick,
    required this.classifyData,
    this.actionButton,
    this.itemActionButton,
    this.loading = false,
  }) : super(key: key);

  final int selectedClassifyIndex;
  final int selectedItemIndex;
  final Function(int classifyIndex, int itemIndex) onItemClick;
  final List<ClassifyData> classifyData;
  final bool loading;
  final Widget Function()? actionButton;
  final Widget Function()? itemActionButton;

  @override
  Widget build(BuildContext context) {
    final bool hasData = classifyData.isNotEmpty;
    return Container(
      decoration: LabBoxDecoration.base,
      child: LabLoadingWrapper(
        margin: EdgeInsets.only(bottom: 80),
        decoration: LabBoxDecoration.base,
        loading: loading,
        child: Padding(
          padding: const EdgeInsets.all(5),
          child: hasData
              ? ListView(
                  children: [
                    for (var i = 0; i < classifyData.length; i++)
                      ClassifyList(
                        title: classifyData[i].classifyName,
                        projects: classifyData[i].data,
                        selectedProjectIndex:
                            i == selectedClassifyIndex ? selectedItemIndex : -1,
                        onProjectClick: (index) {
                          onItemClick(i, index);
                        },
                        actionButton: classifyData[i].showActionButton
                            ? actionButton
                            : null,
                        itemActionButton: classifyData[i].showItemActionButton
                            ? itemActionButton
                            : null,
                      ),
                  ],
                )
              : Center(
                  child: Text(
                    '暂无数据',
                    style: TextStyle(
                      color: LabColors.text600,
                    ),
                  ),
                ),
        ),
      ),
    );
  }
}

class ClassifyData<T> {
  final String classifyName;
  final List<T> data;
  final bool showActionButton;
  final bool showItemActionButton;

  ClassifyData({
    required this.classifyName,
    required this.data,
    this.showActionButton = false,
    this.showItemActionButton = false,
  });
}
