import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/friends/friends_router.dart';
import '../../common/style/style.dart' show AppColors,Constants,ICons;

import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/internal/friends/model/friend_model.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/utils/theme_utils.dart';
// import '../../routers/application.dart';
class ConversationItem extends StatelessWidget {

  ConversationItem(this.conversationItemData,this.fun)
  :assert(conversationItemData != null);
  final Records conversationItemData;
  final Function fun;
  var tapPos;
  @override
  Widget build(BuildContext context) {
    return Material(
      color: ThemeUtils.getTabsBg(context),
      child: InkWell(
        onTap: (){
          print('打开会话:${conversationItemData.userName}');
          fun(conversationItemData.sessionid);
          // conversationItemData.sessionid
          // NavigatorUtils.push(context, "${FriendsRouter.chatRoom}");
          if(conversationItemData.dataTable == 3){
            NavigatorUtils.push(context, "${BbsRouter.chatRoom}?id=${conversationItemData.sessionid}&type=nearToOne&toUserId=${conversationItemData.userId}");
          }else {
            NavigatorUtils.push(context, "${BbsRouter.chatRoom}?id=${conversationItemData.sessionid}&type=oneToOne&toUserId=''");
          }
          // NavigatorUtils.push(context, "${BbsRouter.chatRoom}?id=${conversationItemData.sessionid}&type=oneToOne");
        },
        onTapDown: (TapDownDetails details) {
          tapPos = details.globalPosition;
        },
        onLongPress: (){
          print('hehe');
        },
        child: Container(
          height: ScreenUtil().setHeight(120),
          child: Row(
            mainAxisAlignment:  MainAxisAlignment.center,
            children: <Widget>[
              avatarContainer(conversationItemData),
              Content(),
            ],
          ),
        ),
      ),
    ); 
  }

  Widget Content(){
    return Expanded(
      child: Container(
        height: ScreenUtil().setHeight(130),
        margin: EdgeInsets.only(left:ScreenUtil().setWidth(20.0)),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(width: 0.5,color: Color(AppColors.DividerColor),)
          )
        ),
        child: Row(
          children: <Widget>[
            Title(conversationItemData),
            // Tip(conversationItemData)
          ],
        ),
      ),
    );
  }
  Widget ClipRRectImg(){
    return ClipRRect(
      borderRadius: BorderRadius.circular(50.0),
      child: LoadNetworkImage(conversationItemData.avatarUrl),
    );
  } 

  Widget Avatar(conversationItemData){
    return Container(
      margin: EdgeInsets.only(left:ScreenUtil().setWidth(20.0)),
      child: ClipRRectImg(),
      width: ScreenUtil().setWidth(43),
      height: ScreenUtil().setWidth(43)
    );
  } 

  getCaseType(type){
    String str;
    if(type == 1){
      str = "问诊服务";
    }else if (type == 2){
      str = "出诊服务";
    }else if (type == 3){
      str = "附近的人";
    }
    return str;
  }

  Widget Title(conversationItemData){
    return Expanded(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
          
          Expanded(
                child: Text(
                  conversationItemData.userName??"",
                  style: TextStyle(fontSize: ScreenUtil().setSp(15.0),fontWeight:FontWeight.w400),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
          Tip(conversationItemData)
            ],
          ),
          SizedBox(height: ScreenUtil().setHeight(10.0),),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              
              Container(
                child:Text(
                  conversationItemData.dataTable!=null?getCaseType(conversationItemData.dataTable):"",
                  style: TextStyle(fontSize: ScreenUtil().setSp(12.0),color: Color(0xff0388FD)),
                ),
              ),
              SizedBox(width: ScreenUtil().setWidth(5.0),),
              Expanded(
                child: Text(
                  conversationItemData.name??"",
                  style: TextStyle(fontSize: ScreenUtil().setSp(12.0),color: Color(AppColors.DesTextColor)),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              )
            ],
          ),
          
          SizedBox(height: ScreenUtil().setHeight(10.0),),
          conversationItemData.dataTable!=3? Text(
            "问题描述：" + (conversationItemData.expression!=null?conversationItemData.expression:""),
            style: TextStyle(fontSize: ScreenUtil().setSp(12.0),color: Color(AppColors.DesTextColor)),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ):Container(child:null),
          conversationItemData.dataTable!=3?SizedBox(height: ScreenUtil().setHeight(10.0),):Container(child:null),
          Text(
            conversationItemData.msg??"",
            style: TextStyle(fontSize: ScreenUtil().setSp(12.0),color: Color(AppColors.DesTextColor)),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }

 
  Widget Tip(conversationItemData){
    var _rightArea =<Widget>[
      Text(
        conversationItemData.time!=null?DateUtils.instance.getFormartData(timeSamp: conversationItemData.time,format: "yyyy-MM-dd HH:mm:ss"):(conversationItemData.createTime!=null?DateUtils.instance.getFormartData(timeSamp: conversationItemData.createTime,format: "yyyy-MM-dd HH:mm:ss"):""),
        style:TextStyle(fontSize: ScreenUtil().setSp(12.0),color: Color(AppColors.DesTextColor))),
      // SizedBox(height: ScreenUtil().setHeight(15.0),)
    ];
    // if(conversationItemData.isMute){
    //   _rightArea.add(new Icon(ICons.MUTE_ICON,color: Color(AppColors.ConversationMuteIcon),size: ScreenUtil().setSp(15),));
    // }else{
    //   _rightArea.add(new Icon(ICons.MUTE_ICON,color: Colors.transparent,size: ScreenUtil().setSp(15),));
    // }
    return Container(
      width:ScreenUtil().setWidth(130),
      margin: EdgeInsets.only(right: ScreenUtil().setWidth(10.0)),
      child: Column(
        mainAxisAlignment:  MainAxisAlignment.center,
        children: _rightArea
      ),
    );
  }

  Widget avatarContainer(conversationItemData){
    // if(conversationItemData.isRed == 1){
    //   return Stack(
    //     overflow: Overflow.visible,
    //     children: <Widget>[
    //       Avatar(conversationItemData),
    //       Positioned(
    //         left:13.0 ,
    //         top: -15.0,
    //         child: unreadMsgCountText(conversationItemData),
    //       )
    //     ],
    //   );
    // }else{
    //   return Avatar(conversationItemData);
    // }
    return Avatar(conversationItemData);
  }
  Widget unreadMsgCountText(conversationItemData){
    return Container(
      width: ScreenUtil().setWidth(8.0),
      height: ScreenUtil().setWidth(8.0),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16.0),
        color: Color(AppColors.NotifyDotBg)
      ),
      // child: Text(conversationItemData.statuz.toString(),style:TextStyle(fontSize: ScreenUtil().setSp(15),color: Color(AppColors.NotifyDotText))),
    );
  }

  Widget muteIcon(){
    return new Icon(ICons.MUTE_ICON);
  }
}