import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/profile_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/my_documents/employee_documents_list_model.dart';
import 'package:mohem_flutter_app/ui/screens/my_documents/my_documents_fragment.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';

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

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

class _MyDocumentsScreenState extends State<MyDocumentsScreen> {
  int tabIndex = 0;
  PageController controller = PageController();

  List<EmployeeDocumentsList>? documentsList;

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

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

  void getDocuments() async {
    try {
      documentsList?.clear();
      Utils.showLoading(context);
      documentsList = await ProfileApiClient().getEmployeeDocuments();
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    var requiredDocumentsList;
    var optionalDocumentsList;

    if (documentsList?.isNotEmpty ?? false) {
      requiredDocumentsList = <EmployeeDocumentsList>[];
      optionalDocumentsList = <EmployeeDocumentsList>[];
      documentsList!.forEach((element) {
        if (element.dOCUMENTREQUIREDSTATUS == "Required") {
          requiredDocumentsList.add(element);
        } else {
          optionalDocumentsList.add(element);
        }
      });
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(context, title: LocaleKeys.myDocuments.tr(), showHomeButton: true),
      body: Column(
        children: [
          Container(
            padding: const EdgeInsets.only(left: 21, right: 21, top: 16, bottom: 16),
            decoration: const BoxDecoration(
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(25),
                bottomRight: Radius.circular(25),
              ),
              gradient: LinearGradient(
                transform: GradientRotation(.83),
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: [
                  MyColors.gradiantEndColor,
                  MyColors.gradiantStartColor,
                ],
              ),
            ),
            child: Row(
              children: [myTab(LocaleKeys.requiredDocuments.tr(), 0), myTab(LocaleKeys.optionalDocuments.tr(), 1)],
            ),
          ),
          PageView(
            controller: controller,
            physics: const NeverScrollableScrollPhysics(),
            onPageChanged: (int pageIndex) {
              setState(() {
                tabIndex = pageIndex;
              });
            },
            children: [MyDocumentsFragment(requiredDocumentsList), MyDocumentsFragment(optionalDocumentsList)],
          ).expanded,
        ],
      ),
    );
  }

  Widget myTab(String title, int index) {
    bool isSelected = (index == tabIndex);
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        title.toText12(color: isSelected ? Colors.white : Colors.white.withOpacity(.74), isCenter: true),
        4.height,
        Container(
          height: 8,
          width: 8,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: isSelected ? Colors.white : Colors.transparent,
          ),
        ),
      ],
    ).onPress(() {
      controller.jumpToPage(index);
    }).expanded;
  }
}
