import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_translate/generated/assets.dart';
import 'package:flutter_translate/model/language_model.dart';
import 'package:flutter_translate/pages/home/home_controller.dart';
import 'package:flutter_translate/pages/scene_list/scene_list_controller.dart';
import 'package:get/get.dart';

class LanguageSelector extends GetView<SceneListController> {
  const LanguageSelector({
    super.key,
    required this.isFrom,
  });

  final bool isFrom;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height * 0.6,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(26.r),
          topRight: Radius.circular(26.r),
        ),
      ),
      child: Column(
        children: [
          12.verticalSpace,
          Container(
            width: 28.w,
            height: 4.w,
            decoration: BoxDecoration(
              color: const Color(0xffD9D9D9),
              borderRadius: BorderRadius.circular(2.r),
            ),
          ),
          10.verticalSpace,
          _buildListView(),
        ],
      ),
    );
  }

  Widget _buildListView() {
    final languageModels = <LanguageModel>[
      LanguageModel(code: 'zh-cn', name: 'Chinese (Simplified)'),
      LanguageModel(code: 'ar', name: 'Arabic'),
      LanguageModel(code: 'bn', name: 'Bengali'),
      LanguageModel(code: 'en', name: 'English'),
      LanguageModel(code: 'fr', name: 'French'),
      LanguageModel(code: 'de', name: 'German'),
      LanguageModel(code: 'hi', name: 'Hindi'),
      LanguageModel(code: 'ja', name: 'Japanese'),
      LanguageModel(code: 'jv', name: 'Javanese'),
      LanguageModel(code: 'ko', name: 'Korean'),
      LanguageModel(code: 'pt', name: 'Portuguese'),
      LanguageModel(code: 'pa', name: 'Punjabi'),
      LanguageModel(code: 'ru', name: 'Russian'),
      LanguageModel(code: 'es', name: 'Spanish'),
    ];
    return Expanded(
      child: ListView.builder(
        itemCount: languageModels.length,
        itemBuilder: (context, index) {
          var item = languageModels[index];
          return Material(
            color: Colors.transparent,
            child: InkWell(
              onTap: () => controller.changeLanguage(item, isFrom),
              child: Container(
                padding: const EdgeInsets.symmetric(
                  vertical: 13,
                  horizontal: 33,
                ).w,
                child: GetBuilder<HomeController>(
                  builder: (c) => Row(
                    children: [
                      Expanded(
                        child: Text(
                          item.name,
                          style: TextStyle(
                            color: item.name ==
                                    (isFrom
                                        ? controller.fromLanguage.value.name
                                        : controller.toLanguage.value.name)
                                ? Color(c.appColor.value)
                                : const Color(0xff152A3D),
                            fontSize: 16.sp,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ),
                      Visibility(
                        visible: item.name ==
                            (isFrom
                                ? controller.fromLanguage.value.name
                                : controller.toLanguage.value.name),
                        child: Padding(
                          padding: const EdgeInsets.only(left: 10).w,
                          child: Image.asset(
                            Assets.iconStarGreen,
                            color: Color(c.appColor.value),
                            width: 16.w,
                            height: 16.w,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
