import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tasktracker/Settings/Settings.dart';
import 'package:tasktracker/Data.dart';

class BehaviourSettings extends StatefulWidget {
  const BehaviourSettings({Key? key}) : super(key: key);

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

class _BehaviourSettingsState extends State<BehaviourSettings> {
  bool untracked_unprod = true;
  bool autoUsage = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    init();
  }

  void init() async {
    untracked_unprod = await Settings.getUntrackedUnproductive();
    autoUsage = await Settings.getAutoLog();
    setState(() {});
  }

  TextEditingController untrackedGapSettings = TextEditingController();

  @override
  Widget build(BuildContext context) {
    int untrackedGapValue = int.tryParse(untrackedGapSettings.text) ?? 1;
    return Scaffold(
        appBar: AppBar(title: Text('Behaviour Settings')),
        body: SafeArea(
          child: Container(
              child: Column(
            children: [
              ListTile(
                title: Text("Count untracked time as unproductive"),
                subtitle: Text("Not tracking = Not productive"),
                trailing: Switch.adaptive(
                    value: untracked_unprod,
                    onChanged: (val) {
                      untracked_unprod = val;
                      setState(() {
                        Settings.setUntrackedUnproductive(val);
                      });
                      setState(() {});
                    }),
              ),
              //TODO
              ListTile(
                title: Text("Untracked Delay Threshold"),
                subtitle: Text("Makes it easier to fill in the gaps"),
                trailing: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    if (untrackedGapValue > 0)
                      InkWell(
                        onTap: () {
                          untrackedGapSettings.text =
                              (untrackedGapValue - 1).toString();
                          setState(() {});
                        },
                        child: Container(
                            margin: EdgeInsets.all(10), child: Text('-')),
                      ),
                    SizedBox(
                      width: 25,
                      height: 30,
                      child: TextField(
                        textAlign: TextAlign.center,
                        controller: untrackedGapSettings,
                        keyboardType: TextInputType.number,
                        inputFormatters: <TextInputFormatter>[
                          FilteringTextInputFormatter.digitsOnly
                        ],
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        untrackedGapSettings.text =
                            (untrackedGapValue + 1).toString();
                        setState(() {});
                      },
                      child: Container(
                          margin: EdgeInsets.all(10), child: Text('+')),
                    )
                  ],
                ),
              ),
              ListTile(
                title: Text("Automatically add tasks from Usage Data"),
                subtitle: Text("Requires App Usage access"),
                trailing: Switch.adaptive(
                    value: autoUsage,
                    onChanged: (val) {
                      autoUsage = val;
                      Settings.setAutoLog(val);

                      setState(() {});
                    }),
              ),
            ],
          )),
        ));
  }
}
