import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/items_for_sale/items_for_sale_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/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/items_for_sale/get_sale_categories_list.dart';
import 'package:mohem_flutter_app/models/items_for_sale/item_review_model.dart';
import 'package:mohem_flutter_app/ui/screens/items_for_sale/fragments/add_details_fragment.dart';
import 'package:mohem_flutter_app/ui/screens/items_for_sale/fragments/item_review_fragment.dart';
import 'package:mohem_flutter_app/ui/screens/items_for_sale/fragments/select_category_fragment.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';

class AddNewItemForSale extends StatefulWidget {
  int? pageIndex = 0;
  ItemReviewModel? itemReviewModel;

  AddNewItemForSale({Key? key, this.pageIndex, this.itemReviewModel}) : super(key: key);

  @override
  State<AddNewItemForSale> createState() => _AddNewItemForSaleState();
}

class _AddNewItemForSaleState extends State<AddNewItemForSale> {
  int _currentIndex = 0;
  List<GetSaleCategoriesList> getSaleCategoriesList = [];
  late PageController _controller;
  ItemReviewModel? itemReviewModel;
  int? pageIndex;

  @override
  void initState() {
    _controller = PageController();
    getItemForSaleCategory();
    super.initState();
  }

  void changePageViewIndex(pageIndex) {
    _controller.jumpToPage(pageIndex);
  }

  @override
  Widget build(BuildContext context) {
    pageIndex ??= (ModalRoute.of(context)?.settings.arguments as int?) ?? 0;
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(context, title: LocaleKeys.itemsForSale.tr(), showHomeButton: true),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              showProgress(
                title: LocaleKeys.selectCategory.tr(),
                status: _currentIndex == 0 ? LocaleKeys.inProgress.tr() : (_currentIndex > 0 ? LocaleKeys.completed.tr() : LocaleKeys.locked.tr()),
                color: _currentIndex == 0 ? MyColors.orange : MyColors.greenColor,
                pageIndex: 0,
              ).expanded,
              showProgress(
                title: LocaleKeys.addDetails.tr(),
                status: _currentIndex == 1 ? LocaleKeys.inProgress.tr() : (_currentIndex > 1 ? LocaleKeys.completed.tr() : LocaleKeys.locked.tr()),
                color: _currentIndex == 1 ? MyColors.orange : (_currentIndex > 1 ? MyColors.greenColor : MyColors.lightGrayColor),
                pageIndex: 1,
              ).expanded,
              showProgress(
                title: LocaleKeys.reviewAndSell.tr(),
                status: _currentIndex == 2 ? LocaleKeys.inProgress.tr() : LocaleKeys.locked.tr(),
                color: _currentIndex == 2
                    ? MyColors.orange
                    : _currentIndex > 3
                        ? MyColors.greenColor
                        : MyColors.lightGrayColor,
                isNeedBorder: false,
                pageIndex: 2,
              ),
            ],
          ).paddingAll(21).objectContainerView(disablePadding: true).paddingAll(21),
          PageView(
            physics: const NeverScrollableScrollPhysics(),
            controller: _controller,
            onPageChanged: (index) {
              setState(() {
                _currentIndex = index;
              });
            },
            scrollDirection: Axis.horizontal,
            children: [
              getSaleCategoriesList.isNotEmpty ? SelectCategoryFragment(changePageViewIndex: changePageViewIndex, getSaleCategoriesList: getSaleCategoriesList) : Container(),
              getSaleCategoriesList.isNotEmpty ? AddItemDetailsFragment(changePageViewIndex: changePageViewIndex, selectedSaleCategory: getSaleCategoriesList[0]) : Container(),
              ItemReviewFragment(changePageViewIndex: changePageViewIndex),
            ],
          ).expanded,
        ],
      ),
    );
  }

  Widget showProgress({String? title, String? status, Color? color, bool isNeedBorder = true, int? pageIndex}) {
    return InkWell(
      onTap: () {
        if (_currentIndex > pageIndex!) changePageViewIndex(pageIndex);
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                width: 26,
                height: 26,
                decoration: Utils.containerRadius(color!, 200),
                child: const Icon(
                  Icons.done,
                  color: Colors.white,
                  size: 16,
                ),
              ),
              if (isNeedBorder)
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Utils.mDivider(Colors.grey),
                  ),
                ),
            ],
          ),
          Utils.mHeight(8),
          title!.toText11(),
          Utils.mHeight(2),
          Container(
            padding: const EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
            decoration: Utils.containerRadius(color.withOpacity(0.2), 4),
            child: Text(
              status!,
              style: TextStyle(
                fontSize: 8,
                fontWeight: FontWeight.w600,
                letterSpacing: -0.32,
                color: color,
              ),
            ),
          ),
        ],
      ),
    );
  }

  void getItemForSaleCategory() async {
    try {
      Utils.showLoading(context);
      getSaleCategoriesList = await ItemsForSaleApiClient().getSaleCategories();
      Utils.hideLoading(context);
      setState(() {});
      if (pageIndex == 1) {
        changePageViewIndex(1);
      }
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }
}
