import 'package:flutter/material.dart';

import '../../../../entities/plant_in_row.dart';
import '../../../../repositories/beet.repositories.dart';

class PlantElement extends StatelessWidget {
  final bool showImages;
  final Function(PlantInRow) onRemovePlant;
  final PlantInRow plant;
  final BeetRepository beetRepository;
  final DateTime date;

  const PlantElement({
    Key? key,
    required this.showImages,
    required this.onRemovePlant,
    required this.plant,
    required this.beetRepository,
    required this.date,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(5),
      padding: const EdgeInsets.all(10),
      height: 100,
      width: 160,
      decoration: BoxDecoration(
        color: beetRepository.getBackgroundColorOfPlant(plant, date),
        borderRadius: BorderRadius.circular(8),
      ),
      child: showImages ? _buildImageContent() : _buildTextContent(),
    );
  }

  Widget _buildTextContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Text(
          plant.name,
          textAlign: TextAlign.center,
        ),
        const Divider(),
        Text(beetRepository.getTimeDescription(plant, date)),
        Expanded(child: _buildDropElement()),
      ],
    );
  }

  Widget _buildImageContent() {
    return Row(
      children: [
        Expanded(child: Image.asset(plant.image ?? '')),
        _buildDropElement(),
      ],
    );
  }

  Widget _buildDropElement() {
    return IconButton(
      icon: const Icon(Icons.delete),
      onPressed: () {
        onRemovePlant(plant);
      },
    );
  }
}
