import 'dart:convert';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/provider/chat_provider_model.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheet.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheets/search_employee_bottom_sheet.dart';
import 'package:mohem_flutter_app/widgets/shimmer/dashboard_shimmer_widget.dart';
import 'package:provider/provider.dart';

class ChatHomeScreen extends StatefulWidget {
  @override
  State<ChatHomeScreen> createState() => _ChatHomeScreenState();
}

class _ChatHomeScreenState extends State<ChatHomeScreen> {
  TextEditingController search = TextEditingController();
  late ChatProviderModel data;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    data = Provider.of<ChatProviderModel>(context, listen: false);
    data.registerEvents();
    data.getUserRecentChats();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.white,
      body: Consumer<ChatProviderModel>(
        builder: (BuildContext context, ChatProviderModel m, Widget? child) {
          return m.isLoading
              ? ChatHomeShimmer()
              : Column(
                  children: <Widget>[
                    TextField(
                      controller: m.search,
                      style: const TextStyle(color: MyColors.darkTextColor, fontWeight: FontWeight.w500, fontSize: 12),
                      onChanged: (String val) {
                        m.filter(val);
                      },
                      decoration: InputDecoration(
                        border: fieldBorder(radius: 5, color: 0xFFE5E5E5),
                        focusedBorder: fieldBorder(radius: 5, color: 0xFFE5E5E5),
                        enabledBorder: fieldBorder(radius: 5, color: 0xFFE5E5E5),
                        contentPadding: const EdgeInsets.all(11),
                        hintText: LocaleKeys.searchfromchat.tr(),
                        hintStyle: const TextStyle(color: MyColors.lightTextColor, fontStyle: FontStyle.italic, fontWeight: FontWeight.w500, fontSize: 12),
                        filled: true,
                        fillColor: const Color(0xFFF7F7F7),
                        suffixIconConstraints: const BoxConstraints(),
                        suffixIcon: m.search.text.isNotEmpty
                            ? IconButton(
                                constraints: const BoxConstraints(),
                                onPressed: () {
                                  m.clearSelections();
                                },
                                icon: const Icon(Icons.clear, size: 22),
                                color: MyColors.redA3Color,
                              )
                            : null,
                      ),
                    ).paddingOnly(top: 20, bottom: 14),
                    if (m.searchedChats != null)
                      ListView.separated(
                        itemCount: m.searchedChats!.length,
                        shrinkWrap: true,
                        physics: const ClampingScrollPhysics(),
                        itemBuilder: (BuildContext context, int index) {
                          // todo @aamir, remove list tile, make a custom ui instead
                          return SizedBox(
                            height: 55,
                            child: Row(
                              children: [
                                Stack(
                                  children: <Widget>[
                                    SvgPicture.asset(
                                      "assets/images/user.svg",
                                      height: 48,
                                      width: 48,
                                    ),
                                    Positioned(
                                      right: 5,
                                      bottom: 1,
                                      child: Container(
                                        width: 10,
                                        height: 10,
                                        decoration: BoxDecoration(
                                          color: m.searchedChats![index].userStatus == 1 ? MyColors.green2DColor : Colors.red,
                                          borderRadius: const BorderRadius.all(
                                            Radius.circular(10),
                                          ),
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    (m.searchedChats![index].userName!.replaceFirst(".", " ").capitalizeFirstofEach ?? "").toText14(color: MyColors.darkTextColor).paddingOnly(left: 11, top: 13),
                                  ],
                                ).expanded,
                                SizedBox(
                                  width: 60,
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    mainAxisSize: MainAxisSize.max,
                                    children: <Widget>[
                                      if (m.searchedChats![index].unreadMessageCount! > 0)
                                        Container(
                                          alignment: Alignment.center,
                                          width: 18,
                                          height: 18,
                                          decoration: const BoxDecoration(
                                            color: MyColors.redColor,
                                            borderRadius: BorderRadius.all(
                                              Radius.circular(20),
                                            ),
                                          ),
                                          child: (m.searchedChats![index].unreadMessageCount!.toString())
                                              .toText10(
                                                color: MyColors.white,
                                              )
                                              .center,
                                        ).paddingOnly(right: 10).center,
                                      Icon(
                                        m.searchedChats![index].isFav != null && m.searchedChats![index].isFav == false ? Icons.star_sharp : Icons.star_sharp,
                                        color: m.searchedChats![index].isFav != null && m.searchedChats![index].isFav == true ? MyColors.yellowColor : MyColors.grey35Color,
                                      ).onPress(
                                        () {
                                          if (m.searchedChats![index].isFav == null || m.searchedChats![index].isFav == false) {
                                            m.favoriteUser(
                                              userID: AppState().chatDetails!.response!.id!,
                                              targetUserID: m.searchedChats![index].id!,
                                            );
                                          } else if (m.searchedChats![index].isFav == true) {
                                            m.unFavoriteUser(
                                              userID: AppState().chatDetails!.response!.id!,
                                              targetUserID: m.searchedChats![index].id!,
                                            );
                                          } else {
                                            m.favoriteUser(
                                              userID: AppState().chatDetails!.response!.id!,
                                              targetUserID: m.searchedChats![index].id!,
                                            );
                                          }
                                        },
                                      ).center
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ).onPress(() {
                            Navigator.pushNamed(
                              context,
                              AppRoutes.chatDetailed,
                              arguments: {"targetUser": m.searchedChats![index], "isNewChat": false},
                            ).then((Object? value) {
                              // m.GetUserChatHistoryNotDeliveredAsync(userId: int.parse(AppState().chatDetails!.response!.id.toString()));
                              m.clearSelections();
                              m.notifyListeners();
                            });
                          });
                        },
                        separatorBuilder: (BuildContext context, int index) => const Divider(color: MyColors.lightGreyE5Color).paddingOnly(left: 59),
                      ).paddingOnly(bottom: 70).expanded,
                  ],
                ).paddingOnly(left: 21, right: 21);
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Container(
          width: 60,
          height: 60,
          decoration: const BoxDecoration(
            shape: BoxShape.circle,
            gradient: LinearGradient(
              transform: GradientRotation(.46),
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [
                MyColors.gradiantEndColor,
                MyColors.gradiantStartColor,
              ],
            ),
          ),
          child: const Icon(
            Icons.add,
            size: 30,
            color: MyColors.white,
          ),
        ),
        onPressed: () async {
         // String plainText = 'Muhamad.Alam@cloudsolutions.com.sa';
         // String key = "PeShVmYp";
         // passEncrypt(plainText, "PeShVmYp");
          showMyBottomSheet(
            context,
            callBackFunc: () {},
            child: SearchEmployeeBottomSheet(
              title: LocaleKeys.searchForEmployee.tr(),
              apiMode: LocaleKeys.delegate.tr(),
              fromChat: true,
              onSelectEmployee: (_selectedEmployee) {},
            ),
          );
        },
      ),
    );
  }

  OutlineInputBorder fieldBorder({required double radius, required int color}) {
    return OutlineInputBorder(
      borderRadius: BorderRadius.circular(
        radius,
      ),
      borderSide: BorderSide(
        color: Color(
          color,
        ),
      ),
    );
  }
  //
  // void passEncrypt(String text, String pass) async {
  //   var salt = randomUint8List(8);
  //   var keyndIV = deriveKeyAndIV(pass, salt);
  //   var key = encrypt.Key(keyndIV.item1);
  //   var iv = encrypt.IV(keyndIV.item2);
  //   var encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc, padding: "PKCS7"));
  //   var encrypted = encrypter.encrypt(text, iv: iv);
  //   Uint8List encryptedBytesWithSalt = Uint8List.fromList(createUint8ListFromString("Salted__") + salt + encrypted.bytes);
  //   var resulttt = base64.encode(encryptedBytesWithSalt);
  //   print("Enc : " + resulttt);
  //
  //   decryptAESCryptoJS(resulttt, pass);
  // }
  //
  // Uint8List randomUint8List(int length) {
  //   assert(length > 0);
  //   var random = Random();
  //   var ret = Uint8List(length);
  //   for (var i = 0; i < length; i++) {
  //     ret[i] = random.nextInt(256);
  //   }
  //   return ret;
  // }
  //
  // void decryptAESCryptoJS(String encrypted, String passphrase) {
  //   try {
  //     Uint8List encryptedBytesWithSalt = base64.decode(encrypted);
  //     Uint8List encryptedBytes = encryptedBytesWithSalt.sublist(16, encryptedBytesWithSalt.length);
  //     var salt = encryptedBytesWithSalt.sublist(8, 16);
  //     var keyndIV = deriveKeyAndIV(passphrase, salt);
  //     var key = encrypt.Key(keyndIV.item1);
  //     var iv = encrypt.IV(keyndIV.item2);
  //     var encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc, padding: "PKCS7"));
  //     var decrypted = encrypter.decrypt64(base64.encode(encryptedBytes), iv: iv);
  //     print("Dec : " + decrypted);
  //     //  return decrypted;
  //   } catch (error) {
  //     throw error;
  //   }
  // }

  void enc(String input) {
    var ekey = "PeShVmYp";
    var eIV = "j70IbWYn";
    List<int> eByt = utf8.encode(ekey);
    List<int> eIvByt = utf8.encode(eIV);
    List<int> iByt = utf8.encode(input);



  }

  // ///Accepts encrypted data and decrypt it. Returns plain text
  // String decryptWithAES(String key, Encrypted encryptedData) {
  //   var cipherKey = encrypt.Key.fromUtf8(key);
  //   var encryptService = Encrypter(AES(cipherKey, mode: AESMode.cbc,padding: null));
  //   var initVector = IV.fromUtf8(key.substring(0, 16));
  //   return encryptService.decrypt(encryptedData, iv: initVector);
  // }
  //
  // ///Encrypts the given plainText using the key. Returns encrypted data
  // Encrypted encryptWithAES(String key, String plainText) {
  //   var cipherKey = encrypt.Key.fromUtf8(key);
  //   var encryptService = Encrypter(AES(cipherKey, mode: AESMode.cbc,padding: null));
  //   var initVector = IV.fromUtf8("j70IbWYn");
  //   Encrypted encryptedData = encryptService.encrypt(plainText, iv: initVector);
  //   print(encryptedData.base64);
  //   return encryptedData;
  // }
  //
  // Tuple2<Uint8List, Uint8List> deriveKeyAndIV(String passphrase, Uint8List salt) {
  //   var password = createUint8ListFromString(passphrase);
  //   Uint8List concatenatedHashes = Uint8List(0);
  //   Uint8List currentHash = Uint8List(0);
  //   bool enoughBytesForKey = false;
  //   Uint8List preHash = Uint8List(0);
  //
  //   while (!enoughBytesForKey) {
  //     int preHashLength = currentHash.length + password.length + salt.length;
  //     if (currentHash.length > 0)
  //       preHash = Uint8List.fromList(currentHash + password + salt);
  //     else
  //       preHash = Uint8List.fromList(password + salt);
  //
  //     currentHash = preHash;
  //     concatenatedHashes = Uint8List.fromList(concatenatedHashes + currentHash);
  //     if (concatenatedHashes.length >= 48) enoughBytesForKey = true;
  //   }
  //
  //   var keyBtyes = concatenatedHashes.sublist(0, 32);
  //   var ivBtyes = concatenatedHashes.sublist(32, 48);
  //   return new Tuple2(keyBtyes, ivBtyes);
  // }
  //
  // Uint8List createUint8ListFromString(String s) {
  //   var ret = new Uint8List(s.length);
  //   for (var i = 0; i < s.length; i++) {
  //     ret[i] = s.codeUnitAt(i);
  //   }
  //   return ret;
  // }
  //
  // Uint8List genRandomWithNonZero(int seedLength) {
  //   var random = Random.secure();
  //   const int randomMax = 245;
  //   Uint8List uint8list = Uint8List(seedLength);
  //   for (int i = 0; i < seedLength; i++) {
  //     uint8list[i] = random.nextInt(randomMax) + 1;
  //   }
  //   return uint8list;
  // }
  //
  //
  //
  // void test(String text, String kk) {
  //   Uint8List key = Uint8List.fromList(utf8.encode(kk));
  //   PaddedBlockCipher cipher = exp.PaddedBlockCipherImpl(exp.PKCS7Padding(), exp.ECBBlockCipher(exp.AESEngine()));
  //   cipher.init(true, PaddedBlockCipherParameters<CipherParameters, CipherParameters>(KeyParameter(key), null));
  //   var byte = Uint8List.fromList(utf8.encode(text));
  //   var data = cipher.process(byte);
  //   print(data);
  // }
}
