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/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;

  @override
  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 (int index = 0; index < getUserItemTypesList.length; index++) {
        itemList.add(UpdateUserTypesList(
                itemID: index,
                pITEMTYPE: getUserItemTypesList[index].iTEMTYPE,
                pFYAENABLEDFALG: getUserItemTypesList[index].fYAENABLEDFALG,
                pFYIENABLEDFALG: getUserItemTypesList[index].fYIENABLEDFLAG)
            .toJson());
      }
      updateUserItemTypesList = await WorkListApiClient().updateUserItemTypes(itemList);
      Utils.hideLoading(context);
      Navigator.pop(context);
    } 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),
              ListView.separated(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: getUserItemTypesList.length,
                separatorBuilder: (cxt, index) => 1.divider,
                itemBuilder: (BuildContext context, int index) => customSwitch(getUserItemTypesList[index]),
              ),
            ],
          ).expanded,
          DefaultButton(LocaleKeys.save.tr(), () async {
            updateUserItem();
          }).insideContainer,
        ],
      ),
    );
  }

  Widget customSwitch(GetUserItemTypesList list) {
    return Padding(
      padding: const EdgeInsets.only(top: 8, bottom: 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          list.iTEMTYPE!.toText16(),
          const Spacer(),
          Row(
            children: [
              Transform.scale(
                scale: 0.8,
                child: CupertinoSwitch(
                    trackColor: MyColors.grey57Color,
                    activeColor: MyColors.textMixColor,
                    value: list.fYAENABLEDFALG == 'Y',
                    onChanged: (value) {
                      setState(() {
                        list.fYAENABLEDFALG = value ? 'Y' : 'N';
                      });
                    }),
              ),
              Transform.scale(
                scale: 0.8,
                child: CupertinoSwitch(
                  trackColor: MyColors.grey57Color,
                  activeColor: MyColors.textMixColor,
                  value: list.fYIENABLEDFLAG == 'Y',
                  onChanged: (value) {
                    setState(() {
                      list.fYIENABLEDFLAG = value ? 'Y' : 'N';
                    });
                  },
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}
