import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class pakan extends StatefulWidget {
  const pakan({super.key});

  @override
  State<pakan> createState() => _pakanState();
}

class _pakanState extends State<pakan> {
  bool motorStatus = false;
  String currentDate = "";

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

    DatabaseReference motorStatusRef = FirebaseDatabase.instance
        .reference()
        .child('pakan')
        .child('motor_status');

    motorStatusRef.onValue.listen((event) {
      if (event.snapshot.value != null) {
        setState(() {
          motorStatus = (event.snapshot.value as bool?) ?? false;
        });
      }
    });
  }

  void _updateDate() {
    final now = DateTime.now();
    final formattedDate = DateFormat('EEEE, d MMMM yyyy', 'id_ID').format(now);
    setState(() {
      currentDate = formattedDate;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 30, right: 30, top: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "Hari ini",
                  style: TextStyle(
                    fontSize: 25,
                    fontWeight: FontWeight.w700,
                  ),
                ),
                Text(
                  currentDate,
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 20),
                  padding: EdgeInsets.all(10),
                  width: MediaQuery.of(context).size.width,
                  child: Text(
                    motorStatus
                        ? "Sedang memberi makan ayam"
                        : "Beri makan ayam disini !",
                    style: TextStyle(color: Colors.white),
                  ),
                  decoration: BoxDecoration(
                    color: motorStatus
                        ? Colors.red.withOpacity(0.7)
                        : Color.fromARGB(255, 86, 238, 16).withOpacity(0.7),
                    borderRadius: BorderRadius.circular(5),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 65, bottom: 150),
                  child: Image.asset("images/farm.png"),
                ),
                SizedBox(
                  width: 400,
                  height: 40,
                  child: MaterialButton(
                    elevation: 0,
                    onPressed: () {
                      DatabaseReference databaseReference = FirebaseDatabase
                          .instance
                          .reference()
                          .child('pakan')
                          .child('motor_status');

                      if (motorStatus) {
                        databaseReference.set(false);
                        setState(() {
                          motorStatus = false;
                        });
                      } else {
                        databaseReference.set(true);
                        setState(() {
                          motorStatus = true;
                        });
                      }
                    },
                    child: Text(motorStatus ? "Stop" : "Start",
                        style: TextStyle(
                            color: Color(0xffffffff))), // Mengubah teks tombol
                    color: motorStatus ? Colors.red : Colors.orange,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 102.5),
                  child: Center(
                    child: Text(
                      "Copyright @2023 Team Dev MBKM All Right Reserved",
                      style: TextStyle(fontSize: 11),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
