import 'package:flutter/material.dart';
import 'package:flux_localization/flux_localization.dart';
import 'package:flux_ui/flux_ui.dart';
import 'package:quiver/strings.dart';

import '../../common/config.dart';
import '../../common/constants.dart';
import '../../common/tools.dart';
import '../../models/entities/store_arguments.dart';
import '../common/star_rating.dart';

class StoreItem extends StatelessWidget {
  final Store? store;

  const StoreItem({this.store});

  String get bannerUrl => store?.banner ?? kDefaultStoreBanner;
  String get profileUrl => store?.image ?? kDefaultStoreProfile;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return GestureDetector(
      onTap: () {
        Navigator.pushNamed(
          context,
          RouteList.storeDetail,
          arguments: StoreDetailArgument(store: store),
        );
      },
      child: Container(
        margin: const EdgeInsets.only(left: 15.0, right: 15.0, bottom: 15.0),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6.0),
          color: theme.colorScheme.surface,
          boxShadow: [
            BoxShadow(
              color: theme.colorScheme.primary.withValueOpacity(0.4),
              offset: const Offset(0, 1),
              blurRadius: 3,
            ),
          ],
        ),
        child: Stack(
          children: [
            Column(
              spacing: 20.0,
              children: [
                ClipRRect(
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(6.0),
                    topRight: Radius.circular(6.0),
                  ),
                  child: FluxImage(
                    imageUrl: bannerUrl,
                    fit: ImageTools.boxFit(
                      kVendorConfig.bannerFit,
                      defaultValue: BoxFit.cover,
                    ),
                    height: 120,
                    width: MediaQuery.sizeOf(context).width,
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(15),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(3.0),
                    color: theme.colorScheme.surface,
                  ),
                  child: Row(
                    spacing: 10.0,
                    children: <Widget>[
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            if (isNotBlank(store!.name))
                              Text(
                                store!.name!,
                                style: TextStyle(
                                  fontSize: 18.0,
                                  fontWeight: FontWeight.w600,
                                  color: Theme.of(
                                    context,
                                  ).colorScheme.secondary,
                                ),
                              ),
                            if (isNotBlank(store!.address) &&
                                (store?.showAddress ?? false) &&
                                !kVendorConfig.hideStoreContactInfo) ...[
                              const SizedBox(height: 3),
                              Text(
                                store!.address!,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(
                                  fontSize: 14.0,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ],
                            StoreStatusWidget(store: store!),
                          ],
                        ),
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: <Widget>[
                          SmoothStarRating(
                            allowHalfRating: true,
                            starCount: 5,
                            rating: store?.rating,
                            size: 15.0,
                            color: theme.primaryColor,
                            borderColor: theme.primaryColor,
                            label: (store?.reviewCount?.isNotEmpty ?? false)
                                ? Text(
                                    '(${store?.reviewCount})',
                                    style: theme.textTheme.bodySmall,
                                  )
                                : null,
                            spacing: 0.0,
                          ),
                          const SizedBox(height: 10),
                          Text(
                            S.of(context).visitStore,
                            style: theme.primaryTextTheme.titleMedium!.copyWith(
                              color: Colors.blue,
                              fontWeight: FontWeight.w700,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            if (isNotBlank(profileUrl))
              Positioned(
                top: 90.0,
                left: 10.0,
                child: Container(
                  padding: const EdgeInsets.all(2.5),
                  decoration: BoxDecoration(
                    color: theme.colorScheme.surface,
                    shape: BoxShape.circle,
                  ),
                  child: ClipOval(
                    child: FluxImage(
                      imageUrl: profileUrl,
                      fit: ImageTools.boxFit(
                        kVendorConfig.profileFit,
                        defaultValue: BoxFit.cover,
                      ),
                      height: 60,
                      width: 60,
                    ),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}

class StoreCartItem extends StatelessWidget {
  final Store? store;

  const StoreCartItem({this.store});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.pushNamed(
          context,
          RouteList.storeDetail,
          arguments: StoreDetailArgument(store: store),
        );
      },
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 8.0),
        child: Row(
          children: [
            if (isNotBlank(store!.image))
              Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(16.0),
                  child: ImageResize(
                    url: store!.image,
                    size: kSize.medium,
                    isResize: false,
                    fit: ImageTools.boxFit(
                      kVendorConfig.bannerFit,
                      defaultValue: BoxFit.cover,
                    ),
                    height: 32,
                    width: 32,
                  ),
                ),
              ),
            if (isNotBlank(store!.name))
              Text(
                store!.name!,
                style: TextStyle(
                  fontSize: 15.0,
                  fontWeight: FontWeight.w600,
                  color: Theme.of(context).colorScheme.primary,
                ),
              ),
          ],
        ),
      ),
    );
  }
}

class StoreStatusWidget extends StatelessWidget {
  const StoreStatusWidget({super.key, required this.store});
  final Store store;

  @override
  Widget build(BuildContext context) {
    if (!kAdvanceConfig.showOpeningStatus) {
      return const SizedBox();
    }
    final theme = Theme.of(context);
    return Column(
      children: [
        /// WCFM store condition
        if (store.storeHour != null && store.vacationSettings != null) ...[
          if (store.vacationSettings!.vacationMode) ...[
            if (!store.vacationSettings!.isOpen()) ...[
              const SizedBox(height: 5.0),
              Row(
                children: [
                  const Icon(Icons.schedule, color: Colors.red, size: 12.0),
                  const SizedBox(width: 2.0),
                  Text(
                    S.of(context).onVacation,
                    style: theme.textTheme.bodySmall!.copyWith(
                      color: Colors.red,
                      fontSize: 12.0,
                    ),
                  ),
                ],
              ),
            ] else ...[
              const SizedBox(height: 5.0),
              Row(
                children: [
                  Icon(
                    Icons.schedule,
                    color: store.storeHour!.isOpen()
                        ? Colors.green
                        : Colors.red,
                    size: 12.0,
                  ),
                  const SizedBox(width: 2.0),
                  Text(
                    store.storeHour!.isOpen()
                        ? S.of(context).openNow
                        : S.of(context).closeNow,
                    style: theme.textTheme.bodySmall!.copyWith(
                      color: store.storeHour!.isOpen()
                          ? Colors.green
                          : Colors.red,
                      fontSize: 12.0,
                    ),
                  ),
                ],
              ),
            ],
          ] else ...[
            const SizedBox(height: 5.0),
            Row(
              children: [
                Icon(
                  Icons.schedule,
                  color: store.storeHour!.isOpen() ? Colors.green : Colors.red,
                  size: 12.0,
                ),
                const SizedBox(width: 2.0),
                Text(
                  store.storeHour!.isOpen()
                      ? S.of(context).openNow
                      : S.of(context).closeNow,
                  style: theme.textTheme.bodySmall!.copyWith(
                    color: store.storeHour!.isOpen()
                        ? Colors.green
                        : Colors.red,
                    fontSize: 12.0,
                  ),
                ),
              ],
            ),
          ],
        ],

        /// Dokan store condition
        if (store.storeHour != null && store.vacationSettings == null) ...[
          const SizedBox(height: 5.0),
          Row(
            children: [
              Icon(
                Icons.schedule,
                color: store.storeHour!.isOpen() ? Colors.green : Colors.red,
                size: 12.0,
              ),
              const SizedBox(width: 2.0),
              Text(
                store.storeHour!.isOpen()
                    ? (store.storeOpenNote != null)
                          ? store.storeOpenNote!
                          : S.of(context).openNow
                    : (store.storeCloseNote != null)
                    ? store.storeCloseNote!
                    : S.of(context).closeNow,
                style: theme.textTheme.bodySmall!.copyWith(
                  color: store.storeHour!.isOpen() ? Colors.green : Colors.red,
                  fontSize: 12.0,
                ),
              ),
            ],
          ),
        ],
      ],
    );
  }
}
