import 'dart:io';
import 'dart:typed_data';

import 'package:chat/generated/i18n.dart';
import 'package:chat/models/ChatMsg.dart';
import 'package:chat/utils/screen.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:oktoast/oktoast.dart';


class PhotoPage extends StatefulWidget {
  final MsgModel msg;
  PhotoPage({this.msg});
  @override
  _PhotoPageState createState() => _PhotoPageState();
}

class _PhotoPageState extends State<PhotoPage>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  ImageProvider provider;

  @override
  void initState() {
    super.initState();

    getImgData();
    _controller = AnimationController(vsync: this);
    _controller.addListener(() {
      setState(() {
      });
    });
  }

  getImgData() {
    if (widget.msg.localFile != null) {
      var fileData = File(widget.msg.localFile).readAsBytesSync();
      provider = MemoryImage(fileData);
    } else {
      provider = MemoryImage(Uint8List.fromList(widget.msg.msgContent));
    }
  }

  @override
  void dispose() {
    _controller.stop();
    _controller.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Uint8List fileData;
    if (widget.msg.localFile != null) {

      fileData = File(widget.msg.localFile).readAsBytesSync();
//      print('本地了${fileData.lengthInBytes}');
    } else {
      fileData = Uint8List.fromList(widget.msg.msgContent);
    }
    return Material(color: Colors.black,child: GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        child: Container(
            width: Screen.width,
            height: Screen.height,
            alignment: Alignment.center,
            child: Stack(
              children: <Widget>[
                SingleChildScrollView(
                    child: Column(
                      children: <Widget>[
                        Container(
                            width: Screen.width,
                            constraints: BoxConstraints(minHeight: Screen.height),
                            child: ExtendedImage.memory(
                              fileData,
                              fit: BoxFit.fitWidth,
                              mode: ExtendedImageMode.gesture,
                              initGestureConfigHandler: (state) {
                                return GestureConfig(
                                  minScale: 0.9,
                                  animationMinScale: 0.7,
                                  maxScale: 3.0,
                                  animationMaxScale: 3.5,
                                  speed: 1.0,
                                  inertialSpeed: 100.0,
                                  initialScale: 1.0,
                                  inPageView: true,
                                  initialAlignment: InitialAlignment.center,
                                );
                              },
                            ))
                      ],
                    )),
                Positioned(
                    right: 10,
                    bottom: 10,
                    child: Material(child: InkWell(
                        onTap: saveToGallery,
                        child: Container(
                          width: 50,
                          height: 50,
                          padding: EdgeInsets.all(5),
                          decoration: BoxDecoration(
                              color: Colors.grey.withAlpha(150),
                              borderRadius: BorderRadius.circular(8)),
                          child: Icon(Icons.save_alt, color: Colors.white70),
                        )),))
              ],
            ))),);
  }

  saveToGallery() async {
    if (widget.msg.localFile != null) {
      var data = File(widget.msg.localFile).readAsBytesSync();
      ImageGallerySaver.saveImage(data).then((res) {
        print(res);
        if (res != null) {
          showToast(I18n.of(context).successfully_saved);
        }
      });
    } else {
      ImageGallerySaver.saveImage(Uint8List.fromList(widget.msg.msgContent))
          .then((res) {
        print(res);
        if (res != null) {
          showToast(I18n.of(context).successfully_saved);
        }
      });
    }
  }
}
