import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_translate/global/app_config.dart';
import 'package:flutter_translate/generated/assets.dart';
import 'package:flutter_translate/manager/translate.dart';
import 'package:flutter_translate/pages/home/home_controller.dart';
import 'package:get/get.dart';

class CommonLanguageSelectorBar extends StatelessWidget {
  const CommonLanguageSelectorBar({
    super.key,
    this.isSelect = false,
    this.changeFromLanguage,
    this.changeToLanguage,
  });

  final bool isSelect;
  final Function()? changeFromLanguage;
  final Function()? changeToLanguage;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        _language(true),
        _convertIcon(),
        _language(false),
      ],
    );
  }

  Widget _convertIcon() {
    return GestureDetector(
      onTap: Translate().languageConvert,
      child: Container(
        width: 26.w,
        height: 18.w,
        margin: const EdgeInsets.symmetric(horizontal: 15).w,
        padding: const EdgeInsets.symmetric(
          horizontal: 7,
          vertical: 3,
        ).w,
        decoration: BoxDecoration(
          color: const Color(0xfff1f1f1),
          borderRadius: BorderRadius.circular(9.r),
        ),
        child: Image.asset(Assets.iconConvert),
      ),
    );
  }

  Widget _language(isFrom) {
    final HomeController homeController = Get.put(HomeController());
    return Expanded(
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: isFrom
              ? (changeFromLanguage ??
                  () => Translate().languageChangeBottomSheet(true))
              : (changeToLanguage ??
                  () => Translate().languageChangeBottomSheet(false)),
          child: Obx(() {
            return Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                8.horizontalSpace,
                Flexible(
                  child: Text(
                    isFrom
                        ? Translate().fromLanguageEntity.value.name
                        : Translate().toLanguageEntity.value.name,
                    style: TextStyle(
                      color: !isSelect ||
                          (isFrom
                              ? !Translate().isSelectFromLanguage.value
                              : Translate().isSelectFromLanguage.value)
                          ? const Color(0xff435561)
                          : Color(homeController.appColor.value),
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w500,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ),
                4.horizontalSpace,
                Image.asset(Assets.iconArrowBottom, width: 8.w),
              ],
            );
          }),
        ),
      ),
    );
  }

// void _languageConvert() {
//   var code = Translate().toLanguageEntity.value.code;
//   var name = Translate().toLanguageEntity.value.name;
//
//   Translate().toLanguageEntity.update((fn) {
//     fn?.code = Translate().fromLanguageEntity.value.code;
//     fn?.name = Translate().fromLanguageEntity.value.name;
//   });
//   Translate().fromLanguageEntity.update((fn) {
//     fn?.name = code;
//     fn?.name = name;
//   });
// }

// void _changeFromLanguage() {
//   Translate().isSelectFromLanguage.value = true;
//   Get.bottomSheet(
//     isScrollControlled: true,
//     const CommonLanguageSelector(),
//   );
// }
//
// void _changeToLanguage() {
//   Translate().isSelectFromLanguage.value = false;
//   Get.bottomSheet(
//     isScrollControlled: true,
//     const CommonLanguageSelector(),
//   );
// }
}
