import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/utils.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.dart';
import 'package:tangheem/widgets/common_textfield_widget.dart';

class DiscussionInputDialog extends StatefulWidget {
  final Function(String) onCommentPress;

  DiscussionInputDialog({Key key, this.onCommentPress}) : super(key: key);

  @override
  _DiscussionInputDialogState createState() {
    return _DiscussionInputDialogState();
  }
}

class _DiscussionInputDialogState extends State<DiscussionInputDialog> {
  final TextEditingController _commentController = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  bool hasError = false;
  String errorMessage;
  String otpMessage = "";

  @override
  void initState() {
    super.initState();
    _focusNode.requestFocus();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;

    return Dialog(
      insetPadding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 0.0),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(24),
      ),
      elevation: 0,
      backgroundColor: Colors.transparent,
      child: Directionality(
        textDirection: TextDirection.rtl,
        child: Container(
          width: double.infinity,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(24),
          ),
          padding: EdgeInsets.symmetric(vertical: 24, horizontal: 24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              "اضاف تعليق".toText(18, isBold: true, color: ColorConsts.dark2Text, fontFamily: false),
              SizedBox(height: 12),
              CommonTextFieldWidget(hint: "فحوى التعليق", controller: _commentController, maxLines: isPortrait ? 5 : 3, focusNode: _focusNode, fillColor: Colors.transparent, isBorder: true),
              SizedBox(height: 16),
              Container(
                      height: 40,
                      padding: EdgeInsets.only(left: 16, right: 16),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(30),
                        color: ColorConsts.darkText,
                      ),
                      child: "أضف تعليقك".toText(14, fontFamily: false))
                  .onPress(() {
                if (_commentController.text.length < 1) {
                  Utils.showToast("يجب أن تكتب كلمات قليلة");
                  return;
                }
                _focusNode.unfocus();
                widget.onCommentPress(_commentController.text);
              })
            ],
          ),
        ),
      ),
    );
  }
}
