import 'package:car_provider_app/classes/colors.dart';
import 'package:car_provider_app/config/constants.dart';
import 'package:car_provider_app/extensions/int_extensions.dart';
import 'package:car_provider_app/extensions/string_extensions.dart';
import 'package:car_provider_app/widgets/app_bar.dart';
import 'package:car_provider_app/widgets/button/show_fill_button.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:sizer/sizer.dart';

import '../../../config/routes.dart';
import '../../../generated/locale_keys.g.dart';
import '../../../models/profile/branch.dart';
import '../../../models/profile/branch2.dart';
import '../../../models/profile/categroy.dart';
import '../../../utils/navigator.dart';

class BranchDetailPage extends StatefulWidget {
  ServiceProviderBranch branchData;

  BranchDetailPage(this.branchData);

  @override
  State<BranchDetailPage> createState() => _BranchDetailPageState();
}

class _BranchDetailPageState extends State<BranchDetailPage> {
  List<CategoryData> categories = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widget.branchData.serviceProviderServices!.forEach((element) {
      categories.add(
        new CategoryData(
          id: element.categoryId,
          categoryName: element.categoryName,
          categoryNameN: element.categoryName,
        ),
      );
    });
    categories = categories.toSet().toList();
    print(categories);
    categories.forEach((pelement) {
      widget.branchData.serviceProviderServices!.forEach((element) {
        if (pelement.id == element.categoryId) {
          if (pelement.services == null) pelement.services = [];
          pelement.services!.add(element);
        }
      });
    });
    widget.branchData.categories = categories;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(
        context,
        title: LocaleKeys.branchName.tr(),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                padding: EdgeInsets.all(20),
                child: Column(
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child: titleWidget(icons + "ic_branchs.svg", LocaleKeys.branchInfo),
                        ),
                        IconButton(
                          onPressed: () {
                            navigateWithName(context, AppRoutes.defineBranch, arguments: widget.branchData);
                          },
                          icon: Icon(
                            Icons.edit,
                          ),
                        )
                      ],
                    ),
                    8.height,
                    Column(
                      children: [
                        showData(LocaleKeys.country.tr() + ":", widget.branchData.countryName.toString()),
                        showData(LocaleKeys.city.tr() + ":", widget.branchData.cityName.toString()),
                        showData(LocaleKeys.branchName.tr() + ":", widget.branchData.branchName.toString()),
                        showData(LocaleKeys.branchDescription.tr() + ":", widget.branchData.branchDescription.toString()),
                        showData(LocaleKeys.address.tr() + ":", widget.branchData.address.toString()),
                      ],
                    ),
                    Container(
                      width: double.infinity,
                      height: 1,
                      color: Colors.grey,
                      margin: EdgeInsets.symmetric(vertical: 12),
                    ),
                    categories.length == 0
                        ? LocaleKeys.no_branch.tr().toText12()
                        : ListView.builder(
                            itemBuilder: (context, pIndex) {
                              return Container(
                                width: double.infinity,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    categories[pIndex].categoryName.toString().toText16(isBold: true),
                                    ListView.builder(
                                      itemBuilder: (context, index) {
                                        return Container(
                                          child: ("- " +
                                                  (EasyLocalization.of(context)?.currentLocale?.countryCode == "SA"
                                                      ? categories[pIndex].services![index].serviceNameN.toString()
                                                      : categories[pIndex].services![index].serviceName.toString()))
                                              .toText14(),
                                        );
                                      },
                                      itemCount: categories[pIndex].services?.length,
                                      physics: NeverScrollableScrollPhysics(),
                                      shrinkWrap: true,
                                    )
                                  ],
                                ),
                              );
                            },
                            itemCount: categories.length,
                            physics: NeverScrollableScrollPhysics(),
                            shrinkWrap: true,
                          ),
                  ],
                ),
              ),
            ),
            Container(
              width: double.infinity,
              height: 1,
              color: Colors.grey,
            ),
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: ShowFillButton(
                title: LocaleKeys.editServices,
                maxWidth: double.infinity,
                onPressed: () {
                  navigateWithName(
                    context,
                    AppRoutes.createServices,
                    arguments: widget.branchData,
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget titleWidget(String icon, String title) {
    return Row(
      children: [
        SvgPicture.asset(
          icon,
          width: 18,
          height: 18,
          color: MyColors.darkPrimaryColor,
        ),
        12.width,
        title.toText16(color: Colors.black, isBold: true),
      ],
    );
  }

  Widget showData(String title, String value) {
    return Row(
      children: [
        title.toText10(color: Colors.black, isBold: true),
        8.width,
        value.toText12(
          color: MyColors.textColor,
        )
      ],
    );
  }
}
