//import 'dart:nativewrappers/_internal/vm/lib/core_patch.dart';

import 'package:dashboard/hardware/heartbeatmice.dart';

import 'ui/plot.dart';
import 'package:flutter/material.dart';
import 'util/mouse_cursor.dart';
import 'package:logging/logging.dart';
import 'dart:core';

void main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((record){

      print('[${record.time.hour}:${record.time.minute}:${record.time.second}][${record.level.name}][${record.loggerName}] ${record.message}');
      });
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
    Widget build(BuildContext context) {
      return SoftwareMouseCursor( 
          child : MaterialApp(
            title: 'Heart Beat Mice',
            theme: ThemeData(
              colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
              useMaterial3: true,
              ),
            home: const MyHomePage(title: 'Heart Beat Mice Coba'),
            )
          );
    }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
    State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late HeartBeatMice device;
  Logger log = Logger("_MyHomePageState");
  int index = 1;

  @override
    void initState() {
      super.initState();
      device = HeartBeatMice();
      device.init();
    }
  @override
    void dispose() {
      super.dispose();
      device.dispose();
    }

  Widget myProbe(double height,double width, int index){
    return Column(
        children:  <Widget>[
        Text("Analog $index"),
        Column(children: [
          Row(children: [
            Row(children: [
              IconButton(
                onPressed: () async {device.offset[index-1].wipeUp();} , 
                icon: const Icon(Icons.arrow_drop_up_outlined)),
              const Text("Offset"),
              IconButton(
                onPressed: () async {device.offset[index-1].wipeDown();} , 
                icon: const Icon(Icons.arrow_drop_down_outlined))
            ]),
            Row(children: [
              IconButton(
                onPressed: () async {device.gain[index-1].wipeUp();} , 
                icon: const Icon(Icons.arrow_drop_up_outlined)),
              const Text("Gain"),
              IconButton(
                onPressed: () async {device.gain[index-1].wipeDown();} , 
                icon: const Icon(Icons.arrow_drop_down_outlined)),
            ]),
          ],),
      ],
      ), 
      SizedBox ( 
          height: height.toDouble(),
          width: width,
          child : Plot(
            title: "Analog $index",
            aspectRatio: (height/ width),
            minY: -5,
            maxY: 5,
            maxTimeSeconds: 0.5,
            sampleTimeMicross: 1,
            readAnalog: () async { return device.readAnalog(index); }
            ),
          ),
      ],
      );

  }

  @override
    Widget build(BuildContext context) {
      double height = 300;
      double width = (MediaQuery.of(context).size.width-100);
      return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
            ),
          body: 
          Row(children: [
            IconButton(
              onPressed: () {setState(() {index = ((index -1) < 1 ) ? 1 : index;});} , 
              icon: const Icon(Icons.navigate_before)),
            SizedBox(
              width: width,
              child: myProbe(height, width, index)
              ), 
            IconButton(
              onPressed: () {setState(() {index = ((index +1) > 16) ? 16 : index;});} , 
              icon: const Icon(Icons.navigate_next)),
          ],)
          );
    }
}

//             ListView.builder(
//               itemCount: 1,
//               itemBuilder: (context, i){
//               var index = i+1;
//               return ;
//               },
//               )

//           SizedBox(
//             width: width,
//             child: ListView.builder(
//               itemCount: 4,
//               itemBuilder: (context, i){
//               var index = i+8;
//               return Column(
//                   children:  <Widget>[
//                   Text("Analog $index"),
//                   SizedBox ( 
//                     height: height.toDouble(),
//                     width: width,
//                     child : Plot(
//                       title: "Analog $index",
//                       aspectRatio: (height/ width),
//                       minY: -5,
//                       maxY: 5,
//                       maxTimeSeconds: 0.5,
//                       sampleTimeMicross: 1,
//                       readAnalog: () async { return device.readAnalog(index); }
//                       ),
//                     ),
//                   ],
//                   );
//               },
//               )), 
//           SizedBox(
//             width: width,
//             child: ListView.builder(
//               itemCount: 4,
//               itemBuilder: (context, i){
//               var index = i+4;
//               return Column(
//                   children:  <Widget>[
//                   Text("Analog $index"),
//                   SizedBox ( 
//                     height: height.toDouble(),
//                     width: width,
//                     child : Plot(
//                       title: "Analog $index",
//                       aspectRatio: (height/ width),
//                       minY: -5,
//                       maxY: 5,
//                       maxTimeSeconds: 0.5,
//                       sampleTimeMicross: 1,
//                       readAnalog: () async { return device.readAnalog(index); }
//                       ),
//                     ),
//                   ],
//                   );
//               },
//               )), 
//           SizedBox(
//             width: width,
//             child: ListView.builder(
//               itemCount: 4,
//               itemBuilder: (context, i){
//               var index = i;
//               return Column(
//                   children:  <Widget>[
//                   Text("Analog $index"),
//                   SizedBox ( 
//                     height: height.toDouble(),
//                     width: width,
//                     child : Plot(
//                       title: "Analog $index",
//                       aspectRatio: (height/ width),
//                       minY: -5,
//                       maxY: 5,
//                       maxTimeSeconds: 0.5,
//                       sampleTimeMicross: 1,
//                       readAnalog: () async { return device.readAnalog(index); }
//                       ),
//                     ),
//                   ],
//                   );
//               },
//               )), 


//                  Column(children: [
//                   Row(children: [
//                     Row(children: [
//                       IconButton(
//                         onPressed: () async {device.offset[index].wipeUp();} , 
//                         icon: const Icon(Icons.arrow_drop_up_outlined)),
//                       const Text("Offset"),
//                       IconButton(
//                         onPressed: () async {device.offset[index].wipeDown();} , 
//                         icon: const Icon(Icons.arrow_drop_down_outlined))
//                     ]),
//                     Row(children: [
//                       IconButton(
//                         onPressed: () async {device.gain[index].wipeUp();} , 
//                         icon: const Icon(Icons.arrow_drop_up_outlined)),
//                       const Text("Gain"),
//                       IconButton(
//                         onPressed: () async {device.gain[index].wipeDown();} , 
//                         icon: const Icon(Icons.arrow_drop_down_outlined)),
//                     ]),
//                   ],),
//                    ],
//                    ),