import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/worklist/worklist_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.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/models/get_user_item_type_list.dart';
import 'package:mohem_flutter_app/models/update_user_item_type_list.dart';
import 'package:mohem_flutter_app/models/worklist/update_user_type_list.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';

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

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

class _WorklistSettingsState extends State<WorklistSettings> {
  List<GetUserItemTypesList> getUserItemTypesList = [];
  UpdateUserItemTypesList? updateUserItemTypesList;

  void initState() {
    super.initState();
    userItemTypesList();
  }

  void userItemTypesList() async {
    try {
      Utils.showLoading(context);
      getUserItemTypesList = await WorkListApiClient().getUserItemTypes();
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  void updateUserItem() async {
    try {
      Utils.showLoading(context);
      List<Map<String, dynamic>> itemList = [];
      for (var element in getUserItemTypesList) {
        itemList.add(UpdateUserTypesList(itemID: element.uSERITEMTYPEID, pITEMTYPE: element.iTEMTYPE, pFYAENABLEDFALG: element.fYAENABLEDFALG, pFYIENABLEDFALG: element.fYIENABLEDFLAG).toJson());
      }
      updateUserItemTypesList = await WorkListApiClient().updateUserItemTypes(itemList);
      Utils.hideLoading(context);
      Navigator.pushNamed(context, AppRoutes.workList);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBarWidget(
          context,
          title: LocaleKeys.worklistSettings.tr(),
        ),
        body: Column(
          children: [
            ListView(
              padding: const EdgeInsets.only(top: 21, left: 21, right: 21),
              children: [
                LocaleKeys.TurnNotificationsFor.tr().toText22(color: MyColors.blackColor),
                16.height,
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    LocaleKeys.itemType.tr().toText14(color: MyColors.blackColor),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: ["FYA".tr().toText14(color: MyColors.blackColor), 30.width, "FYI".tr().toText14(color: MyColors.blackColor), 30.width],
                    )
                  ],
                ),
                const Divider(color: MyColors.greyA5Color),
                SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: ListView.builder(
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      physics: ScrollPhysics(),
                      itemCount: getUserItemTypesList == null ? 0 : getUserItemTypesList.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Column(children: [
                          customSwitch(getUserItemTypesList[index]),
                          Divider(
                            color: MyColors.greyC4Color,
                            thickness: 0.5,
                          ),
                        ]);
                      }),
                ),
              ],
            ).expanded,
            DefaultButton(LocaleKeys.save.tr(), () async {
              updateUserItem();
            }).insideContainer,
          ],
        ));
  }

  Widget customSwitch(GetUserItemTypesList list) {
    return Padding(
      padding: const EdgeInsets.only(top: 21),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            list.iTEMTYPE.toString(),
            style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: MyColors.blackColor),
          ),
          const Spacer(),
          Row(
            children: [
              CupertinoSwitch(
                  trackColor: MyColors.grey57Color,
                  activeColor: MyColors.textMixColor,
                  value: list?.fYAENABLEDFALG == 'Y' ? true : false,
                  onChanged: (value) {
                    setState(() {
                      list?.fYAENABLEDFALG = value == true ? 'Y' : 'N';
                    });
                  }),
              CupertinoSwitch(
                  trackColor: MyColors.grey57Color,
                  activeColor: MyColors.textMixColor,
                  value: list?.fYIENABLEDFLAG == 'Y' ? true : false,
                  onChanged: (value) {
                    setState(() {
                      // list.isFYI  = value;
                      list?.fYIENABLEDFLAG = value == true ? 'Y' : 'N';
                    });
                  }),
            ],
          )
        ],
      ),
    );
  }
}
