import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slidable/flutter_slidable.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 '../../friends_router.dart';
import '../near_detail.dart';

class ConversationItem extends StatelessWidget {
  ConversationItem(
      {this.conversationItemData,
      this.onTap,
      this.onDeleting,
      this.onPinning,
      this.onTapAvatar})
      : assert(conversationItemData != null);
  final Records conversationItemData;
  final Function onTap;
  final Function onTapAvatar;
  final Function onDeleting;
  final Function onPinning;
  var tapPos;

  @override
  Widget build(BuildContext context) {
    return Slidable(
      key: UniqueKey(),
      actionPane: SlidableDrawerActionPane(),
      actionExtentRatio: 0.2,
      secondaryActions: <Widget>[
        Container(
          color: Color(0xff555555),
          alignment: Alignment.center,
          child: GestureDetector(
            onTap: () {
              if (onPinning != null) {
                onPinning();
              }
            },
            child: Text(
              '置顶',
              style: TextStyle(fontSize: 14, color: Colors.white),
            ),
          ),
        ),
        Container(
          color: Color(0xffF94F45),
          alignment: Alignment.center,
          child: GestureDetector(
            onTap: () {
              if (onDeleting != null) {
                onDeleting();
              }
            },
            child: Text(
              '删除',
              style: TextStyle(fontSize: 14, color: Colors.white),
            ),
          ),
        )
      ],
      child: Material(
        color: ThemeUtils.getTabsBg(context),
        child: InkWell(
          onTap: () {
            onTap();
          },
          onTapDown: (TapDownDetails details) {
            tapPos = details.globalPosition;
          },
          onLongPress: () {},
          child: Container(
            height: 80,
            decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(width: .5, color: Color(0xffeeeeee))),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                SizedBox(width: 10),
                GestureDetector(
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>
                                NearDetail(friendModel: conversationItemData)));
                  },
                  child: Container(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(45.0),
                      child: LoadNetworkImage(conversationItemData.avatarUrl),
                    ),
                    width: 45,
                    height: 45,
                  ),
                ),
                SizedBox(width: 10),
                Expanded(
                  child: Container(
                    child: contentBody(conversationItemData),
                  ),
                ),
                SizedBox(width: 10),
              ],
            ),
          ),
        ),
      ),
    );
  }

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

  Widget contentBody(conversationItemData) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Row(
          children: <Widget>[
            Text(
              conversationItemData.remarks ??
                  conversationItemData.userName ??
                  "",
              style: TextStyle(
                fontSize: 16,
                color: Colors.black,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
            SizedBox(
              width: 10,
            ),
            Container(
              padding: EdgeInsets.all(2),
              decoration: BoxDecoration(
                color: Color(0xffF3F7FF),
                borderRadius: BorderRadius.circular(4),
              ),
              child: Text(
                conversationItemData.dataTable != null
                    ? getCaseType(conversationItemData.dataTable)
                    : "",
                style: TextStyle(
                  fontSize: 12,
                  color: Color(0xff5589FF),
                ),
              ),
            ),
            Spacer(),
            tips(conversationItemData)
          ],
        ),
        // SizedBox(height: 10),
        // Row(
        //   children: <Widget>[
        //     Text(
        //       conversationItemData.name ?? "",
        //       style: TextStyle(
        //         fontSize: ScreenUtil().setSp(12.0),
        //         color: Colors.black,
        //       ),
        //       maxLines: 1,
        //       overflow: TextOverflow.ellipsis,
        //     ),
        //   ],
        // ),
        // SizedBox(
        //   height: ScreenUtil().setHeight(10.0),
        // ),
        if (conversationItemData.dataTable != 3)
          Text(
            conversationItemData?.expression ?? "",
            style: TextStyle(
              fontSize: 13,
              color: Color(0xff666666),
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        if (conversationItemData.msg != null ||
            conversationItemData.lastMsg != null)
          Text(
            processMsg(
                conversationItemData.lastMsg ?? conversationItemData.msg),
            style: TextStyle(
              fontSize: 13,
              color: Color(0xff666666),
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        if (conversationItemData.isRed == 1)
          Row(
            children: [
              Spacer(),
              unreadMsgCountText(conversationItemData),
            ],
          )
      ],
    );
  }

  String processMsg(String msg) {
    if (msg == null) {
      return "";
    }

    if (msg.endsWith('.aac')) {
      return '「语音」';
    } else
    if (msg.contains('http://')) {
      return '「图片」';
    } else {
      return msg;
    }
  }

  Widget tips(conversationItemData) {
    var _rightArea = <Widget>[
      Text(
          conversationItemData.time != null
              ? DateUtils.instance.getFormartData(
                  timeSamp: conversationItemData.time,
                  format: "yyyy-MM-dd HH:mm:ss")
              : (conversationItemData.lastTime != null
                  ? DateUtils.instance.getFormartData(
                      timeSamp: conversationItemData.lastTime,
                      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 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);
  }
}
