import 'package:farm_tpf/custom_model/Device.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
import 'package:farm_tpf/utils/const_string.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'bloc/device_activity_bloc.dart';

class ListDeviceActivity extends StatefulWidget {
  final int selectedId;
  ListDeviceActivity({required this.selectedId});
  @override
  _ListDeviceActivityState createState() => _ListDeviceActivityState();
}

class _ListDeviceActivityState extends State<ListDeviceActivity> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => DeviceActivityBloc(repository: Repository())..add(DataFetched(selectedId: widget.selectedId)),
      child: HoldInfinityWidget(selectedId: widget.selectedId),
    );
  }
}

class HoldInfinityWidget extends StatelessWidget {
  final int selectedId;
  HoldInfinityWidget({required this.selectedId});
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(backgroundColor: Colors.white, key: _scaffoldKey, appBar: AppBarWidget(), body: InfinityView(selectedId: selectedId));
  }
}

class InfinityView extends StatefulWidget {
  final int selectedId;
  InfinityView({required this.selectedId});
  @override
  _InfinityViewState createState() => _InfinityViewState();
}

class _InfinityViewState extends State<InfinityView> {
  late DeviceActivityBloc _deviceActivityBloc;

  @override
  void initState() {
    _deviceActivityBloc = BlocProvider.of<DeviceActivityBloc>(context);
    _deviceActivityBloc.add(DataFetched(selectedId: widget.selectedId));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<DeviceActivityBloc, DeviceActivityState>(
      builder: (context, state) {
        if (state is DeviceActivityFailure) {
          return const Center(child: Text(label_error_get_data));
        }
        if (state is DeviceActivitySuccess) {
          if (state.items.isEmpty) {
            return const Center(child: Text(label_list_empty));
          }
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Padding(
                padding: EdgeInsets.all(8.0),
                child: Text(
                  'Chọn thiết bị',
                  style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
                ),
              ),
              Expanded(
                child: RefreshIndicator(
                    child: ListView.builder(
                        itemBuilder: (BuildContext context, int index) {
                          return index >= state.items.length ? BottomLoader() : ItemInfinityWidget(item: state.items[index]);
                        },
                        itemCount: state.items.length),
                    onRefresh: () async {
                      _deviceActivityBloc.add(OnRefresh(selectedId: widget.selectedId));
                    }),
              ),
            ],
          );
        }
        return Center(
          child: LoadingListPage(),
        );
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class ItemInfinityWidget extends StatelessWidget {
  final Device item;

  const ItemInfinityWidget({Key? key, required this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Container(
            decoration: const BoxDecoration(border: Border(bottom: BorderSide(color: Colors.grey, width: 0.35))),
            child: RadioListTile(
                title: Text(item.name.toString()),
                value: item,
                groupValue: item.isSelected == false ? null : item,
                onChanged: (Device? value) {
                  Navigator.of(context).pop(value);
                })),
        onTap: () {});
  }
}
