import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class SelectAmountDialog extends StatefulWidget {
  final Function(AmountModel) onValueSelected;
  AmountModel selectedAmount;

  SelectAmountDialog({Key key, this.onValueSelected, this.selectedAmount});

  @override
  _SelectAmountDialogState createState() => _SelectAmountDialogState();
}

class _SelectAmountDialogState extends State<SelectAmountDialog> {
  List<AmountModel> searchAmount = [
    AmountModel(name: "l", nameAr: "لتر", value: 1),
    AmountModel(name: "ml", nameAr: "مم لتر", value: 2),
  ];
  @override
  void initState() {
    super.initState();
    widget.selectedAmount = widget.selectedAmount ?? searchAmount[0];
    getLanguage();
  }

  String languageID = "en";

  void getLanguage() async {
    languageID = await sharedPref.getString(APP_LANGUAGE);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return SimpleDialog(
      children: [
        Column(
          children: [
            Texts(
              TranslationBase.of(context).preferredunit,
              fontSize: 20,
            ),
            Divider(),
            ...List.generate(
              searchAmount.length,
              (index) => Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 2,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: InkWell(
                          onTap: () {
                            setState(() {
                              widget.selectedAmount = searchAmount[index];
                            });
                          },
                          child: ListTile(
                            title: Text(languageID == "ar" ? searchAmount[index].nameAr : searchAmount[index].name),
                            leading: Radio(
                              value: searchAmount[index],
                              groupValue: widget.selectedAmount,
                              activeColor: Colors.red[800],
                              onChanged: (value) {
                                setState(() {
                                  widget.selectedAmount = value;
                                });
                              },
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 5.0,
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 5.0,
            ),
            Row(
              // mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: InkWell(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        child: Center(
                          child: Texts(
                            TranslationBase.of(context).cancel.toUpperCase(),
                            color: Colors.red,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
                Container(
                  width: 1,
                  height: 30,
                  color: Colors.grey[500],
                ),
                Expanded(
                  flex: 1,
                  child: InkWell(
                    onTap: () {
                      widget.onValueSelected(widget.selectedAmount);
                      Navigator.pop(context);
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Center(
                          child: Texts(
                        TranslationBase.of(context).ok,
                        fontWeight: FontWeight.w400,
                      )),
                    ),
                  ),
                ),
              ],
            )
          ],
        )
      ],
    );
  }
}

class AmountModel {
  String name;
  String nameAr;
  int value;

  AmountModel({this.name, this.nameAr, this.value});

  AmountModel.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    nameAr = json['nameAr'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['nameAr'] = this.nameAr;
    data['value'] = this.value;
    return data;
  }
}
