import 'package:flutter/material.dart';

class PadPlacementForm {
  late final TextEditingController _xPosController;
  late final TextEditingController _yPosController;
  final TextEditingController _rotController = TextEditingController(text: '0');
  int _xPos;
  int _yPos;
  int _rot;
  final Offset _playerLoc;

  @override
  String toString() {
    return 'PadPlacementForm($_xPos, $_yPos, $_rot)';
  }

  PadPlacementForm(this._xPos, this._yPos, this._rot, this._playerLoc) {
    _xPosController = TextEditingController(
        text: ((_xPos.toDouble() - _playerLoc.dx.round()) / 2).toString());
    _yPosController = TextEditingController(
        text: ((_yPos.toDouble() - _playerLoc.dy.round()) / 2).toString());
  }

  int get xPos => _xPos;

  int get yPos => _yPos;

  int get rot => _rot;

  Future<void> displayTextInputDialog(BuildContext context) async {
    return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          scrollable: true,
          title: const Text('Pad Placement'),
          content: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
            TextFormField(
              controller: _xPosController,
              decoration: const InputDecoration(
                  helperText:
                      'horizontal position from player to the nearest\nhalf inch'),
            ),
            TextFormField(
              controller: _yPosController,
              decoration: const InputDecoration(
                  helperText:
                      'vertical position from player to the nearest\nhalf inch'),
            ),
            TextFormField(
              controller: _rotController,
              decoration: const InputDecoration(
                  helperText:
                      'clockwise rotation from pointing downward\norientation in whole degrees'),
            ),
          ]),
          actions: <Widget>[
            TextButton(
              child: const Text('CANCEL'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            TextButton(
              child: const Text('OK'),
              onPressed: () {
                _xPos = (double.parse(_xPosController.text) * 2 + _playerLoc.dx)
                    .round();
                _yPos = (double.parse(_yPosController.text) * 2 + _playerLoc.dy)
                    .round();
                _rot = int.parse(_rotController.text);
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    );
  }
}
