import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/friends/friends_router.dart';
import 'package:liftmanager/res/iconfont.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/preview_images.dart';

import '../../common/style/style.dart'
    show AppColors, Constants, ICons, MessageDetailSelects;
import '../component/user_avatat.dart';

class ChatContentView extends StatelessWidget {
  int type; //0 代表对方 ， 1代表自己
  String text; //聊天内容
  String avatar; //头像url
  String username; //昵称
  String remarks;
  int userType; //聊天类型 2群组  1单聊
  int msgType; //类型
  bool isNetwork;
  int contentIndex;
  List<String> urlList;
  int recordingDuration;
  Function onTap;

  ChatContentView(
      {Key key,
      this.type,
      this.text,
      this.avatar,
      this.isNetwork,
      this.username,
      this.remarks,
      this.userType = 2,
      this.msgType,
      this.contentIndex,
      this.urlList,
      this.recordingDuration,
      this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var tapPos;
    //头像组件
    Widget userAvatar = UserAvatar(
      width: 45,
      height: 45,
      image: avatar != '' ? avatar : 'assets/images/temporary/avator2.png',
      isNetwork: isNetwork,
    );

    Widget msgWidget;

    if (msgType == 1) {
      msgWidget = Container(
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(5.0),
            color: type == 0 ? Color(0xffffffff) : Color(0xffE4ECFF),
          ),
          child: Text(
            text,
            style: TextStyle(
              fontSize: 15,
              color: Color(0xff333333),
            ),
          ));
    } else if (msgType == 2) {
      msgWidget = GestureDetector(
        onTap: () {
          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),
        ),
      );
    } else if (msgType == 4) {
      msgWidget = GestureDetector(
        child: Container(
          width: 100,
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(5.0),
            color: type == 0 ? Color(0xffffffff) : Color(0xffE4ECFF),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('${recordingDuration ?? ''} "'),
              Transform.rotate(
                angle: type == 0 ? pi : 0,
                child: Icon(Iconfont.yuyin1, size: 20),
              ),
            ],
          ),
        ),
        onTap: () {
          if (onTap != null) {
            onTap();
          }
        },
      );
    }

    Widget messageTextWidget = InkWell(
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
      },
      onTapDown: (TapDownDetails details) {
        tapPos = details.globalPosition;
      },
      onLongPress: () {},
      child: Container(
          alignment: type == 0 ? Alignment.centerLeft : Alignment.centerRight,
          margin: type == 0
              ? EdgeInsets.only(right: ScreenUtil().setWidth(57))
              : EdgeInsets.only(
                  left: ScreenUtil().setWidth(57),
                ),
          child: msgWidget),
    );

    return Container(
        margin: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: type == 0
              ? [
                  userAvatar,
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: CustomPaint(
                      painter: _ShapesPainter(Colors.white),
                      size: Size(10, 10),
                    ),
                  ),
                  Expanded(child: messageTextWidget),
                ]
              : [
                  Expanded(child: messageTextWidget),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    child: Transform.rotate(
                      angle: -90 * pi / 180,
                      child: CustomPaint(
                        painter: _ShapesPainter(Color(0xffE4ECFF)),
                        size: Size(10, 10),
                      ),
                    ),
                  ),
                  userAvatar
                ],
        ));
  }
}

class _ShapesPainter extends CustomPainter {
  final Color color;

  _ShapesPainter(this.color);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();
    paint.color = color;
    var path = Path();
    path.lineTo(size.width, 0);
    path.lineTo(size.height, size.width);
    path.close();
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

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,
          ),
        );
}
