import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:telnow_mobile_new/src/layouts/mobile/message_broadcast.dart';
import 'package:telnow_mobile_new/src/layouts/components/template.dart';
import 'package:telnow_mobile_new/src/utils/U.dart';

class MobMessageSelectPage extends StatefulWidget {
  final user;
  final tenants;
  const MobMessageSelectPage(this.user, this.tenants, {super.key});

  @override
  State<MobMessageSelectPage> createState() => _MobMessageSelectPageState();
}

class _MobMessageSelectPageState extends State<MobMessageSelectPage> {
  List data = [];
  bool isLoaded = false;
  bool _multiSelectMode = false;
  List<int> selectedIndexes = [];

  getTenant() async {
    List tempData = widget.tenants;
    setState(() {
      isLoaded = true;
      if (tempData.length != 0) {
        data.addAll(tempData);
      }
    });
  }

  _selectedIndex(i) {
    if (selectedIndexes.contains(i)) {
      selectedIndexes.remove(i);
    } else {
      selectedIndexes.add(i);
    }
    setState(() {
      _multiSelectMode = selectedIndexes.length == 0 ? false : true;
    });
  }

  _getDataTenants() {
    List tempData = [];
    selectedIndexes.forEach((e) {
      tempData.add(data[e]);
    });

    return tempData;
  }

  @override
  void initState() {
    // TODO: implement initState
    getTenant();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var bodyWidth = U.bodyWidth(context);
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: appBarTemplate(context: context, title: 'selectRecipient'.tr()),
      body: !isLoaded ? loadingTemplate() : Column(
        children: [
          divider(),
          Expanded(
            child: Container(
              alignment: Alignment.topCenter,
              width: bodyWidth,
              child: ListView(
                children: List.generate(data.length, (i) {
                  return GestureDetector(
                    child: Container(
                      child: ListTile(
                        leading: Container(
                          height: 40,
                          width: 40,
                          child: Center(
                            child: _multiSelectMode && selectedIndexes.contains(i) ? Icon(
                              Icons.check,
                              color: Colors.white,
                            ) : Text(data[i]['name'][0], style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
                          ),
                          decoration: BoxDecoration(shape: BoxShape.circle, color: Color(U.getColor(data[i]['code']))),
                        ),
                        title: Text(data[i]['name']),
                        subtitle: Text(data[i]['description'], style: TextStyle(fontSize: 13.0)),
                      ),
                      decoration: BoxDecoration(color: _multiSelectMode && selectedIndexes.contains(i) ? Colors.black12 : Colors.white)
                    ),
                    onTap: () async {
                      if(_multiSelectMode){
                        _selectedIndex(i);
                      }
                      else{
                        navigateTo(context, MobMessageBroadcastPage(widget.user, [data[i]]));
                        // Navigator.of(context).pop();
                      }
                    },
                    onLongPress: () {
                      setState(() {
                        _multiSelectMode = true;
                        _selectedIndex(i);
                      });
                    },
                  );
                }),
              ),
            ),
          )
        ],
      ),
      floatingActionButton: _multiSelectMode ? FloatingActionButton(
        child: U.iconsax('arrow-right'),
        backgroundColor: primaryColor,
        onPressed: () async {
          final value = await navigateTo(context, MobMessageBroadcastPage(widget.user, _getDataTenants()));
          if (value == 'pop') {
            Navigator.pop(context);
          }
        },
      ):null,
    );
  }
}
