import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/consts.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/ui/screens/items_for_sale/fragments/items_for_sale.dart';
import 'package:mohem_flutter_app/ui/screens/items_for_sale/fragments/my_posted_ads_fragment.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';

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

  @override
  State<ItemsForSale> createState() => _ItemsForSaleState();
}

class _ItemsForSaleState extends State<ItemsForSale> {
  int tabIndex = 0;
  PageController controller = PageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(context, title: LocaleKeys.itemsForSale.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.itemsForSale.tr(), 0), myTab(LocaleKeys.myPostedAds.tr(), 1)],
            ),
          ),
          PageView(
            controller: controller,
            physics: const NeverScrollableScrollPhysics(),
            onPageChanged: (int pageIndex) {
              setState(() {
                tabIndex = pageIndex;
              });
            },
            children: [ItemsForSaleFragment(), MyPostedAdsFragment()],
          ).expanded,
        ],
      ),
      floatingActionButton: Container(
        height: 50,
        width: 50,
        decoration: const BoxDecoration(
          shape: BoxShape.circle,
          gradient: LinearGradient(transform: GradientRotation(.83), begin: Alignment.topRight, end: Alignment.bottomLeft, colors: [
            MyColors.gradiantEndColor,
            MyColors.gradiantStartColor,
          ]),
        ),
        child: const Icon(Icons.add, color: Colors.white, size: 30),
      ).onPress(
        () {
          Utils.removeStringFromPrefs(SharedPrefsConsts.editItemForSale);
          Navigator.pushNamed(context, AppRoutes.addNewItemForSale);
        },
      ),
    );
  }

  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(() {
          setState(() {
            // showFabOptions = true;
          });
        })
      ],
    ).onPress(() {
      controller.jumpToPage(index);
    }).expanded;
  }
}
