import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/int_extensions.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';

class AppLanguageChange extends StatefulWidget {
  AppLanguageChange({Key? key}) : super(key: key);

  @override
  _AppLanguageChangeState createState() {
    return _AppLanguageChangeState();
  }
}

class _AppLanguageChangeState extends State<AppLanguageChange> {
  String? selectedValue;

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

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

  @override
  Widget build(BuildContext context) {
    selectedValue ??= context.locale.languageCode;
    return Column(
      spacing: 24.h,
      children: [
        Container(
          padding: EdgeInsets.only(top: 4, bottom: 4),
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
          child: Column(
            children: [
              languageItem("English".needTranslation, "en"),
              1.divider,
              languageItem("العربية".needTranslation, "ar"),
            ],
          ),
        ),
        CustomButton(
            text: LocaleKeys.save.tr(),
            onPressed: () {
              context.setLocale(selectedValue == 'en' ? Locale('en', 'US') : Locale('ar', 'SA')).then((val) {
                Navigator.pop(context);
              });
            }),
      ],
    );
  }

  Widget languageItem(String title, String _value) {
    return SizedBox(
      height: 72,
      child: Row(
        spacing: 8.h,
        children: [
          Radio(
            value: _value,
            groupValue: selectedValue,
            activeColor: AppColors.errorColor,
            onChanged: (value) {
              setState(() {
                selectedValue = _value;
              });
            },
            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          title.toText16(weight: FontWeight.w500, textOverflow: TextOverflow.ellipsis, maxlines: 1).expanded,
        ],
      ).paddingOnly(left: 16, right: 16).onPress(() {
        setState(() {
          selectedValue = _value;
        });
      }),
    );
  }
}
