import 'dart:io';

import 'package:eitc_erm_dental_flutter/generated/assets.dart';
import 'package:eitc_erm_dental_flutter/pages/history/vm/history_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HistoryItemView extends StatelessWidget {
  final int index;
  final String name;
  final String path;
  final int type;

  const HistoryItemView(
      {super.key,
      required this.index,
      required this.name,
      required this.path,
      required this.type});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
            child: Stack(
          children: [
            type == 0
                ? SizedBox.expand(
                    child: ClipRRect(
                    borderRadius: BorderRadius.circular(10.r),
                    child: Image(
                      image: FileImage(File(path)),
                      fit: BoxFit.cover,
                      errorBuilder: (ctx, _, __) => const Center(
                        child: Icon(Icons.image_not_supported_outlined),
                      ),
                    ),
                  ))
                : Container(
                    decoration: BoxDecoration(
                        color: Colors.black87,
                        borderRadius: BorderRadius.circular(10.r)),
                    alignment: Alignment.center,
                    child: Opacity(
                      opacity: 0.5,
                      child: Icon(
                        size: 28.r,
                        Icons.play_circle,
                        color: Colors.white,
                      ),
                    ),
                  ),
            Consumer(builder: (ctx, ref, _) {
              bool isSelectMode = ref.watch(historySelectModeProvider);
              bool isSelected = ref.watch(historyItemSelectedProvider(path));
              return isSelectMode
                  ? Positioned(
                      right: 5.r,
                      bottom: 5.r,
                      child: Image.asset(
                        isSelected
                            ? Assets.imagesIconChecked
                            : Assets.imagesIconUnchecked,
                        width: 24.r,
                        height: 24.r,
                      ))
                  : SizedBox();
            }),
          ],
        )),
        SizedBox(
          height: 8.h,
        ),
        Text(
          name,
          maxLines: 1,
          style: TextStyle(fontSize: 12.sp),
        )
      ],
    );
  }
}
