import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/visits/visit.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/visits/visit_status.dart';

class VisitItem extends StatelessWidget {
  final Visit visit;
  final int index;
  final bool isSelected;
  final bool activeSelectMod;
  final Function(Visit) onPressed;
  final Function(Visit) onLongPress;
  final Function(Visit) onSelect;

  const VisitItem({Key key, this.visit, this.onPressed, this.isSelected = false, this.activeSelectMod = false, this.onLongPress, this.onSelect, this.index}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserProvider userProvider = Provider.of<UserProvider>(context);
    Color itemColor = index % 2 == 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.onPrimary;
    Color onItemColor = index % 2 != 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.onPrimary;
    Subtitle subtitle = AppLocalization.of(context).subtitle;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
          backgroundColor: itemColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(AppStyle.getBorderRadius(context)),
          ),
        ),
        onPressed: () {
          if (activeSelectMod && userProvider.user.type == UsersTypes.engineer) {
            onSelect(visit);
          } else {
            onPressed(visit);
          }
        },
        onLongPress: userProvider.user.type == UsersTypes.engineer
            ? () {
                onLongPress(visit);
              }
            : null,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Expanded(
                  child: Text(
                    "S.N: ${visit.deviceSerialNumber}" ?? "No serial number",
                    style: Theme.of(context).textTheme.titleLarge.copyWith(color: onItemColor, fontSize: 16, fontWeight: FontWeight.bold),
                  ),
                ),
                AnimatedSwitcher(
                  duration: const Duration(milliseconds: 400),
                  child: Visibility(
                    key: ValueKey(activeSelectMod),
                    visible: activeSelectMod,
                    child: SizedBox(
                      height: 18,
                      child: Checkbox(
                          activeColor: Colors.black38,
                          value: isSelected,
                          onChanged: (value) {
                            onSelect(visit);
                          }),
                    ),
                  ),
                ),
                VisitStatusLabel(
                  visit: visit,
                )
              ],
            ),
            Divider(
              color: onItemColor,
            ),
            Text(
              visit.deviceNumber ?? "",
              style: Theme.of(context).textTheme.titleMedium.copyWith(color: onItemColor, fontSize: 14),
            ),
            Divider(
              color: onItemColor,
            ),
            Row(
              children: [
                Expanded(
                  child: Text(
                    visit.hospitalName ?? "No client found",
                    style: Theme.of(context).textTheme.titleMedium.copyWith(
                          color: onItemColor,
                          fontSize: 14,
                        ),
                  ),
                ),
              ],
            ),
            Text(
              visit.modelAndBrand ?? "",
              style: Theme.of(context).textTheme.titleMedium.copyWith(
                    color: onItemColor,
                    fontSize: 14,
                  ),
            ),
            if (visit.employName != null && visit.employName.isNotEmpty) Divider(color: onItemColor),
            if (visit.employName != null && visit.employName.isNotEmpty)
              Row(
                children: [
                  Expanded(
                    child: Text(
                      visit.employName ?? "No employ found",
                      style: Theme.of(context).textTheme.titleLarge.copyWith(color: onItemColor, fontSize: 14, fontWeight: FontWeight.bold),
                    ),
                  ),
                  // Text(
                  //   visit.contactStatus ?? "",
                  //   style: Theme.of(context).textTheme.subtitle1.copyWith(
                  //       color: onItemColor,
                  //       fontSize: 14,
                  //   ),
                  // ),
                ],
              ),
            Divider(
              color: onItemColor,
            ),
            Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        subtitle.expectDate,
                        style: Theme.of(context).textTheme.titleLarge.copyWith(color: onItemColor, fontSize: 14),
                      ),
                      Text(
                        visit.expectDate ?? subtitle.noDateFound,
                        style: Theme.of(context).textTheme.titleLarge.copyWith(color: onItemColor, fontSize: 14),
                      ),
                    ],
                  ),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      subtitle.actualDate,
                      style: Theme.of(context).textTheme.titleLarge.copyWith(color: onItemColor, fontSize: 14),
                    ),
                    Text(
                      visit.actualDate ?? subtitle.noDateFound,
                      style: Theme.of(context).textTheme.titleLarge.copyWith(color: onItemColor, fontSize: 14),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
