import 'package:flutter/material.dart';

import '../entities/plant.dart';
import '../entities/plant_in_row.dart';
import '../repositories/beet.repositories.dart';
import 'content_widgets/control_bar.dart';
import 'content_widgets/dashboard.dart';
import 'content_widgets/footer.dart';
import 'content_widgets/new_beet_row.dart';

class Content extends StatefulWidget {
  final BeetRepository beetRepository;

  const Content({required this.beetRepository, Key? key}) : super(key: key);

  @override
  ContentState createState() => ContentState();
}

class ContentState extends State<Content> {
  bool showSpaceRequirements = false;
  bool showImages = false;
  bool isSidebarOpen = true;
  DateTime selectedDate = DateTime.now();

  @override
  void initState() {
    super.initState();
  }

  void toggleSidebar() {
    setState(() {
      isSidebarOpen = !isSidebarOpen;
    });
  }

  void saveBeet() {
    setState(() {
      widget.beetRepository.saveBeet();
    });
  }

  void plantDroppedOnRow(int rowId, Plant plant) {
    setState(() {
      widget.beetRepository.addPlantToRowById(rowId, plant);
    });
  }

  void newRow() {
    setState(() {
      widget.beetRepository.addNewRowToBeet();
    });
  }

  void newDaySelected(DateTime date) {
    setState(() {
      selectedDate = date;
    });
  }

  void plantRemovedFromRow(int rowId, PlantInRow plantInRow) {
    setState(() {
      widget.beetRepository.removePlantFromRowById(rowId, plantInRow.position);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Control(
          showSpaceRequirements: showSpaceRequirements,
          onShowSpaceChanged: (value) {
            setState(() {
              showSpaceRequirements = value;
            });
          },
          showImages: showImages,
          onImagesChanged: (value) {
            setState(() {
              showImages = value;
            });
          },
          actionIsNeeded: widget.beetRepository.isActionNeeded(selectedDate),
        ),
        Expanded(
            child: Column(children: [
          Dashboard(
              onPlantDroppedToRow: plantDroppedOnRow,
              beetRepository: widget.beetRepository,
              showSpaceRequirement: showSpaceRequirements,
              showImages: showImages,
              currentDate: selectedDate,
              onPlantRemoveFromRow: plantRemovedFromRow),
          if (widget.beetRepository.newRowAllowed) NewBeetRow(onNewRow: newRow)
        ])),
        Footer(
          onNewDaySelected: newDaySelected,
          beetRepository: widget.beetRepository,
          date: selectedDate,
        )
      ],
    );
  }
}
