import 'package:chat/data/UserData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:chat/proto/all.pbserver.dart';

// class TranslateState {
//   static const int NoPerson = 0; //没有翻译人员在线
//   static const int SameLan = 1; //同一语言

//   static const int Unopen = 2; //没有开启人工翻译
//   static const int Opend = 3; //已经开启人工翻译
// }

class TranslateSateWidget extends StatefulWidget {
  final int friendId;
  TranslateSateWidget({this.friendId});
  @override
  _TranslateSateWidgetState createState() => _TranslateSateWidgetState();
}

class _TranslateSateWidgetState extends State<TranslateSateWidget> {
  //翻译相关

  TranslateState translateStatus = TranslateState.SameLan;

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

    getTranslateState();

    MessageMgr().on("Update Translate State", updateTranslateState);
  }

  @override
  void dispose() {
    MessageMgr().off("Update Translate State", updateTranslateState);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Color transColor = Colors.grey;
    if (translateStatus == TranslateState.Opend) {
      transColor = Constants.ConfrimButtonColor;
    } else if (translateStatus == TranslateState.Unopen) {
      transColor = Constants.BlackTextColor;
    }

    bool switchTrans = translateStatus == TranslateState.Unopen ||
        translateStatus == TranslateState.Opend;
    if (!switchTrans) {
      return Container();
    }
    return Center(
        child: InkWell(
            child: Container(
                decoration: BoxDecoration(
                  border: Border.all(color: transColor),
                  borderRadius: BorderRadius.circular(3),
                ),
                padding: EdgeInsets.symmetric(horizontal: 7, vertical: 2),
                child: fixedText(I18n.of(context).translate,
                    fontSize: 11, color: transColor)),
            onTap: () async {
              if (UserData().isSuperVip) {
                print('开启/关闭人工翻译');
                switchTranslateState();
              } else {
                showToast(I18n.of(context).need_svip);
              }
            }));
  }

  //更改人工翻译状态
  switchTranslateState() async {
    TranslateState newState;
    if (translateStatus == TranslateState.Opend) {
      newState = TranslateState.Unopen;
    } else {
      newState = TranslateState.Opend;
    }
    bool res = await HttpUtil().updateTranslateState(
        widget.friendId, newState == TranslateState.Opend);
    if (res) {
      translateStatus = newState;
      if (mounted) {
        setState(() {});
      }
//      MsgHandler.swtichTranslateState(newState);
    }
  }

  updateTranslateState(transArgs) {
    print('切换人工翻译');

    if (transArgs.hTransl == TranslateState.Opend &&
        translateStatus != transArgs.hTransl) {
      showToast(I18n.of(context).translate_on);
    } else if (transArgs.hTransl == TranslateState.Unopen) {
      //showToast(I18n.of(context).translate_off);
    }
    translateStatus = transArgs.hTransl;
    if (mounted) {
      setState(() {});
    }
  }

  //获取人工翻译的状态
  Future getTranslateState() async {
    print('~~~~~~~~~~~获取聊天翻译状态~~~~~~~~~~~~~~');
    Map tranObj = await HttpUtil().getTranslateState(widget.friendId);
    if (tranObj != null) {
      int status = tranObj['status'];

      translateStatus = TranslateState.valueOf(status);
      //同步双方翻译状态
//      MsgHandler.swtichTranslateState(translateStatus);
      if (mounted) {
        setState(() {});
      }
    }
  }
}
