import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_bloc.dart';
import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart';
import 'package:zhiying_base_widget/widgets/search_result/search_input/model/search_result_input_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'dart:ui';
import 'package:flutter_bloc/flutter_bloc.dart';

///
/// 搜索结果页面输入框
///
class SearchResultInputWidget extends StatefulWidget {
  final Map<String, dynamic> data;
  SearchResultInputModel model;

  SearchResultInputWidget(this.data, {Key key}) : super(key: key) {
    try {
      model = SearchResultInputModel.fromJson(jsonDecode(data['data']));
    } catch (e) {
      Logger.error(e);
    }
  }

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

class _SearchResultInputWidgetState extends State<SearchResultInputWidget> {
  TextEditingController _textEditingController;
  FocusNode _focusNode;

  /// 返回事件
  void _openPop() {
    Navigator.maybePop(context);
  }

  /// 搜索事件
  void _onSearchButtomClick() {
    print('搜索商品');
    String keywords = _textEditingController?.text?.toString()?.trim();
    if(!EmptyUtil.isEmpty(keywords)){
      // BlocProvider.of<SearchResultGoodsListBloc>(context).add(SearchResultGoodsListSubmitEvent(keywords));
    }
  }

  /// 输入变化
  // void _onChanged(String text){
  //   if(!EmptyUtil.isEmpty(text)){
  //     BlocProvider.of<SearchThinkBloc>(context).add(SearchThinkKeyWrodsChangeEvent(text));
  //   }else{
  //     BlocProvider.of<SearchThinkBloc>(context).add(SearchThinkShowBaseViewEvent());
  //   }
  // }

  @override
  void initState() {
    _textEditingController = TextEditingController(text: widget?.model?.keywords ?? '');
    _focusNode = FocusNode();
    super.initState();
  }

  @override
  void dispose() {
    _focusNode?.unfocus();
    _focusNode?.dispose();
    _textEditingController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _getMainWidget(widget?.model),
    );
  }

  /// 主视图
  Widget _getMainWidget(SearchResultInputModel model) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(/*left: 12.5,*/ right: 12.5, top: MediaQueryData.fromWindow(window).padding.top),
      // child: _getAppBar(model),
      child: Row(
        children: <Widget>[
          /// 返回键
          _createReturnWidget(model),

          /// 输入框
          Expanded(child: _createInputWidget(model)),

          /// 搜索按钮
          _createSubmitButtomWidget(model),
        ],
      ),
    );
  }

  /// 返回按钮
  Widget _createReturnWidget(SearchResultInputModel model) {
    return IconButton(
      icon: Icon(
        Icons.arrow_back_ios,
        size: 22,
        color: HexColor.fromHex('#000000'),

      ),
      onPressed: () => _openPop(),
    );
  }

  /// 搜索按钮
  Widget _createSubmitButtomWidget(SearchResultInputModel model) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: ()=> _onSearchButtomClick(),
      child: Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.only(
          top: 6,
          bottom: 6,
          left: 10, /*right: 12.5*/
        ),
        child: Text(
          model?.search_button ?? '搜索',
          style: TextStyle(fontSize: 14, color: HexColor.fromHex(model?.search_button_color), fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  /// 输入框
  Widget _createInputWidget(SearchResultInputModel model) {
    return Container(
      width: double.infinity,
      child: Container(
        height: 32,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(30),
          color: HexColor.fromHex('#F9F9F9'),
        ),
        padding: const EdgeInsets.only(left: 12.5, right: 12.5),
        child: TextField(

          onTap: ()=> Navigator.maybePop(context),
          // onChanged: (str) => _onChanged(str),
          showCursor: true,
          cursorWidth: 1,
          onSubmitted: (text) => _onSearchButtomClick(),
          controller: _textEditingController,
          focusNode: _focusNode,
          style: TextStyle(fontSize: 14, color: HexColor.fromHex('#333333')),
          decoration: InputDecoration(
            filled: false,
            // focusColor: Colors.transparent,
            // fillColor: Colors.transparent,
            border: InputBorder.none,
            focusedBorder: InputBorder.none,
            focusedErrorBorder: InputBorder.none,
            errorBorder: InputBorder.none,
            disabledBorder: InputBorder.none,
            enabledBorder: InputBorder.none,
            hintText: model?.search_inpu_hint_text ?? '搜索更多优惠商品',
            hintStyle: TextStyle(color: HexColor.fromHex('#999999'), fontSize: 14),
          ),
        ),
      ),
    );
  }

}
