import 'dart:convert';
import 'dart:typed_data';

import 'package:chat/data/UserData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/map/location_result.dart';
import 'package:chat/utils/app_navigator.dart';
import 'package:chat/utils/screen.dart';
import 'package:chat/utils/upload_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:menu/menu.dart';

class PlaceItem extends StatelessWidget {
  final bool isMe;
  final List<int> placeContent;
  PlaceItem({this.isMe, this.placeContent});
  @override
  Widget build(BuildContext context) {
    LocationResult location =
        LocationResult.fromJson(json.decode(utf8.decode(placeContent)));

    print('地图截屏大小：${location.screen.length}');
    var provider = MemoryImage(Uint8List.fromList(location.screen));

    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: Container(
        width: Screen.width - 160,
        child: Column(
          children: <Widget>[
            AddressTitle(address: location.address),
            GestureDetector(
              child: Container(
                height: 100,
                width: double.infinity,
                child: Image(fit: BoxFit.cover, image: provider),
              ),
              onTap: () {
                print('点击地图');
                AppNavigator.pushMapPage(context, location);
              },
            )
          ],
        ),
      ),
    );
  }
}

class AddressTitle extends StatefulWidget {
  final String address;
  AddressTitle({this.address});
  @override
  _AddressTitleState createState() => _AddressTitleState();
}

class _AddressTitleState extends State<AddressTitle> {
  String transTitle;
  int curLan = 0;
  List<String> transList = [];

  Map translateMap = {};

  String translateTitle;
  bool isTranslating = false;

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

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        InkWell(
            onTap: isTranslating ? null : _changeTitle,
            child: Menu(
                items: [
                  MenuItem(I18n.of(context).copy, () {
                    ClipboardData clipboardData = new ClipboardData(
                        text: translateTitle ?? widget.address);
                    Clipboard.setData(clipboardData);
                  }),
                ],
                child: Container(
                    padding: EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                    width: Screen.width - 160,
                    color: Colors.white,
                    child: Text(translateTitle ?? widget.address, textScaleFactor: 1.0,)))),
        Positioned(
            right: 0,
            bottom: 0,
            child: isTranslating
                ? Container(
                    width: 10,
                    height: 10,
                    padding: EdgeInsets.all(1.0),
                    child: CircularProgressIndicator(
                      strokeWidth: 1.0,
                    ))
                : InkWell(
                    child: Icon(
                      IconData(0xe6a5, fontFamily: Constants.IconFontFamily),
                      color: Color(0xFF3FBAFD),
                      size: 20,
                    ),
                    onTap: _changeTitle))
      ],
    );
  }

  _changeTitle() async {
    curLan += 1;
    curLan %= 3;
    var lang;
    switch (curLan) {
      case 0:
        //lang = 'zh-cn';
        lang = LanguageType.SimplifiedChinese;
        break;
      case 1:
        //lang = 'en';
        lang = LanguageType.English;
        break;
      case 2:
        //lang = 'vi';
        lang = LanguageType.Vietnamese;
        break;
      default:
    }

    if (translateMap[lang] != null) {
      translateTitle = translateMap[lang];
      isTranslating = false;
      setState(() {});
      return;
    }
    try {
      isTranslating = true;
      print('开始翻译 to $lang');
      setState(() {});

      UploadUtil()
          .commitTranslateSource(1, UserData().language, lang, widget.address)
          .then((title) {
        print('翻译结果 to $title');
        if (title != null) {
          translateTitle = title;
          if (lang == LanguageType.SimplifiedChinese) {
            //替换street
            translateTitle = translateTitle.replaceAll(
                RegExp('street', caseSensitive: false), '街道');
            translateTitle = translateTitle.replaceAll(
                RegExp('Binh Tan', caseSensitive: false), '平新郡');
            translateTitle = translateTitle.replaceAll(
                RegExp('Tan Tao', caseSensitive: false), '新桃村');
          }
          translateMap[lang] = translateTitle;
        }

        isTranslating = false;
        if (mounted) {
          setState(() {});
        }
      });
    } catch (e) {
      print('翻译错误 $e');
    }
  }
}
