import 'package:boilerplate/core/extensions/number_extension.dart';
import 'package:boilerplate/core/i18n/translations.g.dart';
import 'package:boilerplate/core/styles/my_colors.dart';
import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/core/styles/my_font.dart';
import 'package:boilerplate/domain/entities/product/product_data.dart';
import 'package:boilerplate/presentation/widgets/ipm_cart_button.dart';
import 'package:boilerplate/presentation/widgets/ipm_favorite_button.dart';
import 'package:boilerplate/presentation/widgets/ipm_intellectual_tag.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_share/flutter_share.dart';

import '../../../../../core/di/injection_init.dart';
import '../../../../../domain/entities/flavor/flavor_data.dart';
import '../../../../bloc/product_detail/product_detail_bloc.dart';
import 'product_highlight.dart';

class ProductHeader extends StatelessWidget {
  final ProductData data;
  final bool? isFav;
  final bool? isInCart;
  final int userId;
  const ProductHeader(
    this.data, {
    Key? key,
    this.isFav,
    this.isInCart,
    required this.userId,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: MyDimens.dimen20,
        right: MyDimens.dimen16,
        left: MyDimens.dimen16,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              IPMIntellectualTag(
                tagName: data.intellectualType?.name ?? '',
              ),
              SizedBox(width: MyDimens.dimen10),
              // _interestedPerson(),
            ],
          ),
          SizedBox(height: MyDimens.dimen16),
          Text(
            data.name ?? "",
            style: MyFont.bold30,
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              _favorite(context),
              _cart(context),
              Spacer(),
              _share(),
            ],
          ),
          Divider(
            color: Color(0xFFEAEAEA),
            height: MyDimens.dimen20 * 2,
          ),
          ProductHighlight(data),
          Divider(
            color: Color(0xFFEAEAEA),
            height: MyDimens.dimen20 * 2,
          ),
          _typeCategory(),
        ],
      ),
    );
  }

  Widget _typeCategory() {
    if ((data.category ?? []).isEmpty && data.intellectualType == null) {
      return SizedBox();
    }

    List<Map<String, String>> list = [];

    if (data.intellectualType != null) {
      list.add({
        AppLocalizations.products.product_type:
            data.intellectualType?.name ?? ""
      });
    }

    if ((data.category ?? []).isNotEmpty) {
      list.add({
        AppLocalizations.products.product_category:
            data.category?.first.name ?? "",
      });
    }

    if (data.price != null && data.price!.isNotEmpty) {
      list.add({
        AppLocalizations.products.price: data.price ?? "",
      });
    }

    return Column(
      children: [
        Column(
          children: list
              .map((e) => Padding(
                    padding: EdgeInsets.only(bottom: MyDimens.dimen10),
                    child: Row(
                      children: [
                        Expanded(
                          flex: 6,
                          child: Text(
                            e.entries.first.key,
                            style: MyFont.bold16,
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child: Text(
                            e.entries.first.value,
                            style: MyFont.regular16,
                          ),
                        ),
                      ],
                    ),
                  ))
              .toList(),
        ),
        Divider(
          color: Color(0xFFEAEAEA),
          height: MyDimens.dimen10 * 2,
        ),
      ],
    );
  }

  Widget _cart(BuildContext context) {
    // if (userId == 0) {
    //   return Container();
    // }
    return Container(
      margin: EdgeInsets.only(left: MyDimens.dimen14),
      child: IPMCartButton(
          initialValue: isInCart ?? false,
          onChanged: (change) {
            BlocProvider.of<ProductDetailBloc>(context).add(
              ChangeProductCartEvent(
                change,
              ),
            );
          }),
    );
  }

  Widget _favorite(BuildContext context) {
    // if (userId == 0) {
    //   return Container();
    // }
    return Container(
      margin: EdgeInsets.only(left: MyDimens.dimen14),
      child: IPMFavoriteButton(
          initialValue: isFav ?? false,
          onChanged: (change) {
            BlocProvider.of<ProductDetailBloc>(context).add(
              ChangeProductFavoriteEvent(
                change,
              ),
            );
          }),
    );
  }

  Widget _share() {
    return GestureDetector(
      onTap: () {
        // print("onshare => ${data.name}");
        // print(
        //     "onshare => ${data.name}\n\n${getIt.get<AppFlavorData>().webDomain}/product/${data.id}");
        FlutterShare.share(
          title: "${data.name}",
          text:
              '${data.name}\n\n${getIt.get<AppFlavorData>().webDomain}/product/${data.id}',
        );
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(
            Icons.share_outlined,
            color: MyColor.grey,
            size: MyDimens.dimen14,
          ),
          SizedBox(width: MyDimens.dimen4),
          Text(
            AppLocalizations.products.share,
            style: MyFont.regular12.copyWith(
              color: MyColor.grey,
              height: 1.8,
            ),
            textAlign: TextAlign.center,
          )
        ],
      ),
    );
  }

  Widget _commentCount() {
    return Expanded(
      child: Text(
        AppLocalizations.products.qty_of_comment(
          qty: 200.toPriceFormat(hasDicemal: false),
        ),
        style: MyFont.regular12.copyWith(
          color: MyColor.orange,
          decoration: TextDecoration.underline,
          height: 1.8,
        ),
      ),
    );
  }

  Widget _rating() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Icon(
          Icons.star_outline,
          color: MyColor.orange,
          size: MyDimens.dimen20,
        ),
        SizedBox(width: MyDimens.dimen4),
        Text(
          data.reviewStatus ?? '0',
          style: MyFont.regular12.copyWith(
            color: MyColor.orange,
            height: 1.8,
          ),
          textAlign: TextAlign.center,
        )
      ],
    );
  }

  Widget _interestedPerson() {
    return Text(
      AppLocalizations.products.interested_person(
        qty: 1241.toPriceFormat(hasDicemal: false),
      ),
      style: MyFont.regular12.copyWith(
        color: MyColor.orange,
        decoration: TextDecoration.underline,
      ),
    );
  }
}
