import 'package:cpt_community/modules/community/newsfeed_detail/newsfeed_detail_vm.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:widgets/my_text_view.dart';

/// 多行输入框

class CommentsTextareaInput extends HookConsumerWidget {
  bool showCounter = true;
  bool autoFocus = false;
  CommentsTextareaInput({Key? key,this.showCounter = true, this.autoFocus = false}): super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final state = ref.watch(newsfeedDetailVmProvider);
    final vm = ref.read(newsfeedDetailVmProvider.notifier);
    final noteCount = useState(0);

    useEffect((){
      return (){
        vm.resetCommentField();
        Log.d('dispose');
      };
    },[]);
    return _buildTextAreaLayout(context, ref, state, vm, noteCount);
  }

  Widget _buildTextAreaLayout(BuildContext context, ref, state, vm , noteCount){
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Expanded(
          child: TextField(
            cursorColor: context.appColors.authFiledText,
            cursorWidth: 1.5,
            autofocus: autoFocus,
            textAlign: TextAlign.start,
            enabled: true,
            maxLines: null,
            expands: true,
            focusNode: state.commentFieldInfo['focusNode'],
            controller: state.commentFieldInfo!['controller'],
            decoration: InputDecoration(
              isDense: true,
              isCollapsed: true,
              border: InputBorder.none,
              hintText: state.commentFieldInfo!['hintText'],
              hintStyle: TextStyle(
                color: context.appColors.authFiledHint,
                fontSize: 14.0,
                fontWeight: FontWeight.w300,
              ),
            ),
            style: TextStyle(
              color: context.appColors.authFiledText,
              fontSize: 14.0,
              fontWeight: FontWeight.w300,
            ),
            textInputAction: TextInputAction.done,
            onSubmitted: (value) {
              // FocusScope.of(context).unfocus();
            },
            onChanged: (text) {
              // 当文本改变时，更新字符数量
              Log.d("text  $text");
              Log.d("文本改变  ${state.commentFieldInfo!['controller']?.text}");
              if(showCounter){
                noteCount.value = text.length;
              }
            },
          ),
        ),
      ],
    );
  }
}

