import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'input_text.dart';

class LengthPicker extends StatefulWidget {
  final TextEditingController controller;
  final String lableKey;
  final bool isReadOnly;

  const LengthPicker(
      {Key key, this.controller, this.lableKey, this.isReadOnly = false})
      : super(key: key);

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

class _LengthPickerState extends State<LengthPicker> {
  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    if (widget.controller != null) {
      _setText();
      widget.controller.addListener(() {
        _setText();
      });
    }
  }

  _setText() {
    double v = double.parse(widget.controller.text, (s) => 0);
    int f = (v / 12).floor();
    int ins = (v % 12).round();

    _controller.text = "$f' $ins\"";
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: widget.isReadOnly ? null : () => _showDialog(context),
      child: Padding(
          padding: EdgeInsets.only(left: 8, right: 8),
          child: InputText(
            enabled: false,
            labelTextKey: widget.lableKey,
            controller: _controller,
            textInputType: TextInputType.number,
            textAlign: TextAlign.end,
          )),
    );
  }

  void _showDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return LengthPickerDialog(
          controller: widget.controller,
          labelKey: widget.lableKey,
        );
      },
    );
  }
}

class LengthPickerDialog extends StatefulWidget {
  final TextEditingController controller;
  final String labelKey;

  const LengthPickerDialog({Key key, this.controller, this.labelKey})
      : super(key: key);

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

class _LengthPickerDialogState extends State<LengthPickerDialog> {
  int _valueFeet;
  int _valueInc;
  @override
  void initState() {
    super.initState();
    _valueFeet = 0;
    _valueInc = 0;
    if (widget.controller != null) {
      double v = double.parse(widget.controller.text, (s) => 0);
      _valueFeet = (v / 12).floor();
      _valueInc = (v % 12).toInt();
    }
  }

  @override
  Widget build(BuildContext context) {
    return SimpleDialog(
      title: Center(
          child: LocalText(
        context,
        widget.labelKey,
        color: primaryColor,
        fontSize: 16,
      )),
      children: [
        Center(child: Text(_getText())),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 8.0, left: 8),
              child: LocalText(
                context,
                "feet",
                color: primaryColor,
                fontSize: 12,
              ),
            ),
            Slider(
              activeColor: primaryColor,
              value: _valueFeet.toDouble(),
              min: 0,
              max: 15,
              divisions: 100,
              label: (_valueFeet ?? 0).round().toString(),
              onChanged: (double v) {
                _updateFeet(v);
              },
            ),
          ],
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 8.0, left: 8),
              child: LocalText(
                context,
                "inch",
                color: primaryColor,
                fontSize: 12,
              ),
            ),
            Slider(
              activeColor: primaryColor,
              value: _valueInc.toDouble(),
              min: 0,
              max: 11,
              divisions: 100,
              label: (_valueInc ?? 0).round().toString(),
              onChanged: (double v) {
                _updateInc(v);
              },
            ),
          ],
        )
      ],
    );
  }

  _updateFeet(double v) {
    setState(() {
      _valueFeet = v.toInt();
    });
    if (widget.controller != null) {
      int _v = _valueInc.round() + _valueFeet.round() * 12;
      widget.controller.text = _v.toString();
    }
  }

  _updateInc(double v) {
    setState(() {
      _valueInc = v.toInt();
    });
    if (widget.controller != null) {
      int _v = _valueInc.round() + _valueFeet.round() * 12;
      widget.controller.text = _v.toString();
    }
  }

  String _getText() {
    int ft = _valueFeet.round();
    int ins = _valueInc.round();
    return "$ft\' $ins\"";
  }
}
