import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../common/style/style.dart' show AppColors,Constants,ICons,MessageDetailSelects;
import '../component/user_avatat.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'dart:convert';
import 'package:liftmanager/widgets/preview_images.dart';

class ChatContentView extends StatelessWidget {
  int type; //0 代表对方 ， 1代表自己
  String text;//聊天内容
  String avatar;//头像url
  String username;//昵称
  int userType;//聊天类型 2群组  1单聊
  int msgType;//类型
  bool isNetwork;
  int contentIndex;
  List<String>urlList;
  ChatContentView({Key key, this.type, this.text,this.avatar,this.isNetwork,this.username,this.userType=2,this.msgType,this.contentIndex,this.urlList})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var tapPos;
    //头像组件
    Widget userAvatar =  UserAvatar(
      padding: EdgeInsets.only(
        top: ScreenUtil().setHeight(5),
        right: (type == 0 ? 0.0 : ScreenUtil().setWidth(15)),
        left: (type == 0 ? ScreenUtil().setWidth(15) : 0.0)
      ),
      width: ScreenUtil().setWidth(40),
      height: ScreenUtil().setWidth(40),
      image: avatar!='' ? avatar: 'assets/images/temporary/avator2.png',
      isNetwork: isNetwork,
      onPressed: () {print('点击头像');}
    );
    
    Widget userNameWidget =   Container(
        margin: EdgeInsets.only(left: type == 0 ? ScreenUtil().setWidth(20) : 0,bottom: ScreenUtil().setHeight(10),right: type == 0 ? 0: ScreenUtil().setWidth(20)),
        child: Text(username,style: TextStyle(fontSize: ScreenUtil().setSp(14.0)),),
      );
    Widget messageTextWidget = InkWell(
      onTapDown: (TapDownDetails details) {
        tapPos = details.globalPosition;
      },
      onLongPress: (){
        print('hehe');
      },
      child: Container(
        margin: type == 0 ? EdgeInsets.only(left:ScreenUtil().setWidth(10),right: ScreenUtil().setWidth(57)) :EdgeInsets.only(left:ScreenUtil().setWidth(57),right: ScreenUtil().setWidth(10)),
        child:msgType==1? Text(text,
          // style: TextStyle(fontSize: ScreenUtil().setSp(15.0),color: Color(0xff999999),height: 1.3),
          style: TextStyle(fontSize: ScreenUtil().setSp(15.0),color: Color(AppColors.TextBobule),height: 1.3),
        ):GestureDetector(
          onTap: (){
            // print(JsonEncoder().convert(urlList));
            // print(123456);
            // print("////////////////////////////");
            // print(text);
            for(int i=0;i<urlList.length;i++){
              if(urlList[i] == text){
                // print(urlList[i]);
                Navigator.of(context).push(
                                  new FadeRoute(
                                    page: PhotoViewGalleryScreen(
                                      images: urlList, //传入图片list
                                      index: i, //传入当前点击的图片的index
                                      // heroTag: img,//传入当前点击的图片的hero tag （可选）
                                    ),
                                  ),
                                );
                return;
              }
            }
           
          },
          child: LoadNetworkImage(
                                              text,
                                              width: ScreenUtil().setWidth(100),
                                              // height: ScreenUtil().setWidth(120),
                                            ),
        ),
        padding: EdgeInsets.only(left:ScreenUtil().setWidth(8),right:ScreenUtil().setWidth(8),bottom:ScreenUtil().setHeight(8),top:ScreenUtil().setHeight(8)),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5.0),
          color: type == 0 ? Color(0xfff5f5f5) : Color(0xfff5f5f5),
          boxShadow: [
                      BoxShadow(
                        offset: Offset(0, 0), //x,y轴
                        color: Colors.grey[300], //投影颜色
                        blurRadius: 2, //投影距离
                      )
                    ],
        ),
      ),
    );
    
    final List<Widget>  nameAndText = [
        userNameWidget,
        messageTextWidget
    ];
    final List<Widget>  onlyText = [
        messageTextWidget
    ];
    textBubble(){
      return Expanded(
        child: Column(
          crossAxisAlignment: type == 0 ? CrossAxisAlignment.start : CrossAxisAlignment.end,
          children: userType == 2 && type == 0 ? nameAndText : onlyText,
        )
      );
    }
    return Container(
      margin: EdgeInsets.only(bottom: ScreenUtil().setHeight(10.0),top: ScreenUtil().setHeight(10.0)),
      child: type == 0 ?
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          userAvatar,
          textBubble()
        ],
      ) :
      Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          textBubble(),
          userAvatar,
        ],
      ) ,
    );
    
  }
}

class FadeRoute extends PageRouteBuilder {
  final Widget page;
  FadeRoute({this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) =>
              FadeTransition(
            opacity: animation,
            child: child,
          ),
        );
}