import 'dart:io';
import 'dart:async';
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gme_flutter/tmg_sdk.dart';
import 'package:gme_flutter/tmg_type.dart';
import 'realVoice.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class PttVoicePage extends StatefulWidget {
  PttVoicePage({Key? key}) : super(key: key);

  @override
  _PttVoicePageState createState() {
    return _PttVoicePageState();
  }
}

class _PttVoicePageState extends State<PttVoicePage> {
  _PttVoicePageState() {
    MyCallback.getInstance().eventHandler = handleEventMsg;
  }
  final _editMaxFlieLen = TextEditingController(text: '10000');
  double _micValue = 100;
  double _spkValue = 100;
  String strCallInfo = "call info";
  String strCurLanguage = "cmn-Hans-CN";
  String strTransLanguage = "cmn-Hans-CN";
  String strVoiceName = "cmn-CN-Standard-A";
  String strReordFormat = ".ogg";
  String _filePath = "";
  String _fileId = "";
  int _fileSize = 0;
  int _fileDuration = 0;
  int _micVolume = 0;
  int _spkVolume = 0;
  int _micLevel = 0;
  int _spkLevel = 0;
  ITMG_VOICE_TYPE _nVoiceType = ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_ORIGINAL_SOUND;
  final _editTranslateText = TextEditingController(text: '');
  final _editTranslateOrSpeechResult = TextEditingController(text: '');
  final FocusNode focusNode = FocusNode();
  bool _switchRecordStart = false;
  bool _switchPlayRecordFile = false;
  bool _switchRecordPause = false;
  bool _switchStreamStart = false;
  Timer? _micLevelTimer;
  Timer? _spkLevelTimer;
  //FToast? fToast;
  void showCupertinoAlertDialog(String strTitle, String strBody) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          title: Text("$strTitle"),
          content: Column(
            children: <Widget>[
              SizedBox(height: 10),
              Align(child: Text("$strBody"), alignment: Alignment(0, 0)),
            ],
          ),
          actions: <Widget>[
            CupertinoDialogAction(
              child: Text("ok"),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    );
  }

  void handleEventMsg(int eventType, String data) {
    if(eventType == ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_USER_VOLUMES || eventType == ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_USER_UPDATE || eventType == ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_NETWORK_QUALITY) {
      return;
    }
    strCallInfo = data;
     setState(() {
          strCallInfo;
        });
    if (eventType ==
        ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_PTT_STREAMINGRECOGNITION_IS_RUNNING) {
      var curJson = json.decode(data);
      setState(() {
        _editTranslateOrSpeechResult.text = curJson["text"];
      });
    } else if (eventType ==
        ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_PTT_STREAMINGRECOGNITION_COMPLETE) {
      var curJson = json.decode(data);
      setState(() {
        _editTranslateOrSpeechResult.text = curJson["text"];
      });
    } else if (ITMG_MAIN_EVENT_TYPE.ITMG_MAIN_EVENT_TYPE_PTT_UPLOAD_COMPLETE ==
        eventType) {
      var curJson = json.decode(data);
      _fileId = curJson["file_id"];
    } else if (ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_PTT_SPEECH2TEXT_COMPLETE ==
        eventType) {
      var curJson = json.decode(data);
      setState(() {
        _editTranslateOrSpeechResult.text = curJson["text"];
      });
    } else if (ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_PTT_DOWNLOAD_COMPLETE ==
        eventType) {
      var curJson = json.decode(data);
      int result = curJson["result"];
      if (result == 0) {
        _showToast("Download successful");
      } else {
        _showToast("Download failed, check error code");
      }
    } else if (ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_PTT_TRANSLATE_TEXT_COMPLETE ==
        eventType) {
      var curJson = json.decode(data);
      int result = curJson["result"];
      if (0 == result) {
        var translateResult = curJson["target_text"];
        var translateJson = json.decode(translateResult);
        translateResult = translateJson["target_text"];
        setState(() {
          _editTranslateOrSpeechResult.text =
              translateResult[0]["translated_text"];
        });
      } else {
        _showToast(
          "Text-to-speech failed, please check the error code $result",
        );
      }
    } else if (ITMG_MAIN_EVENT_TYPE
            .ITMG_MAIN_EVENT_TYPE_PTT_TEXT2SPEECH_COMPLETE ==
        eventType) {
      var curJson = json.decode(data);
      int result = curJson["result"];
      if (0 == result) {
        _fileId = curJson["file_id"];
        _showToast("Text To Speech successful,Please download the file");
      } else {
        _showToast(
          "Text-to-speech failed, please check the error code $result",
        );
      }
    }
  }

  Future<void> micLevelTimerStart() async {
    _micLevelTimer = Timer.periodic(Duration(milliseconds: 30), (
      Timer timer,
    ) async {
      final int res =
          await ITMGContext.GetInstance().GetAudioCtrl().GetMicLevel();
      setState(() {
        _micLevel = res;
      });
    });
  }

  Future<void> spkLevelTimerStart() async {
    _spkLevelTimer = Timer.periodic(Duration(milliseconds: 30), (
      Timer timer,
    ) async {
      final int res =
          await ITMGContext.GetInstance().GetAudioCtrl().GetSpeakerLevel();
      setState(() {
        _spkLevel = res;
      });
    });
  }

  @override
  void initState() {
    super.initState();
    //  fToast = FToast();
    //  fToast!.init(context);
  }

  _showToast(String msg) {
    return;
  }

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

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      // Center is a layout widget. It takes a single child and positions it
      // in the middle of the parent.
      scrollDirection: Axis.vertical,
      reverse: false,
      controller: ScrollController(),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,

        children: <Widget>[
          Text(""),
          TextField(
            controller: _editMaxFlieLen,
            decoration: const InputDecoration(
              icon: Icon(Icons.filter_1_outlined),
              labelText: 'Max File Len',
              labelStyle: TextStyle(color: Colors.blueGrey, fontSize: 20),
              enabledBorder: OutlineInputBorder(
                /*边角*/
                borderRadius: BorderRadius.all(
                  Radius.circular(30), //边角为30
                ),
                borderSide: BorderSide(
                  color: Colors.blueGrey, //边线颜色为黄色
                  width: 2, //边线宽度为2
                ),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.blueGrey, //边框颜色为绿色
                  width: 2, //宽度为5
                ),
              ),
            ),
            onEditingComplete: () {
              FocusScope.of(context).requestFocus(FocusNode());
              var fileLen = int.tryParse(_editMaxFlieLen.text);

              if (fileLen == null) {
                showCupertinoAlertDialog(
                  "Invalid data",
                  "Voice length over 58 seconds or less than 1 second or invalid characters",
                );
                return;
              }
              ITMGContext.GetInstance().GetPTT().SetMaxMessageLength(fileLen);
            },
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              DropdownButton<String>(
                items: [
                  DropdownMenuItem(
                    child: Text('Chinese Mandarin'),
                    value: "cmn-Hans-CN",
                  ),
                  DropdownMenuItem(
                    child: Text('English American'),
                    value: "en-US",
                  ),
                  DropdownMenuItem(
                    child: Text('Cantonese'),
                    value: "yue-Hant-HK",
                  ),
                  DropdownMenuItem(child: Text('Japanese'), value: "ja-JP"),
                  DropdownMenuItem(
                    child: Text('Chinese Shanghai'),
                    value: "wuu-SH",
                  ),
                ],
                value: strCurLanguage,
                icon: Icon(Icons.language_outlined),
                onChanged: (value) {
                  setState(() {
                    if (value == null) return;
                    strCurLanguage = value;
                  });
                },
              ),
            ],
          ),

          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              DropdownButton<String>(
                items: [
                  DropdownMenuItem(child: Text('ogg'), value: ".ogg"),
                  DropdownMenuItem(child: Text('mp3'), value: ".mp3"),
                ],
                value: strReordFormat,
                icon: Icon(Icons.library_music_sharp),
                onChanged: (value) {
                  setState(() {
                    if (value == null) return;
                    strReordFormat = value;
                  });
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'startRecord',
                style: Theme.of(context).textTheme.titleSmall,
              ),
              Switch(
                value: _switchRecordStart,
                onChanged: (value) async {
                  if (value) {
                    if (Platform.isAndroid || Platform.operatingSystem == 'ohos') {
                      bool status = await Permission.microphone.isGranted;
                      if (!status) {
                        await Permission.microphone.request().isGranted;
                      }
                    }
                    final pathObj = await getApplicationDocumentsDirectory();
                    String filePath = pathObj.path + "/record" + strReordFormat;
                    int res = await ITMGContext.GetInstance()
                        .GetPTT()
                        .StartRecording(filePath);
                    if (res != 0) {
                      showCupertinoAlertDialog(
                        "Invalid operation",
                        "Please check the error code $res",
                      );
                      return;
                    }
                    _filePath = filePath;
                    micLevelTimerStart();
                  } else {
                    ITMGContext.GetInstance().GetPTT().StopRecording();
                    _micLevelTimer!.cancel();
                  }
                  setState(() {
                    _switchRecordStart = value;
                  });
                },
              ),

              TextButton.icon(
                icon: Icon(Icons.event_seat_sharp),
                onPressed: () async {
                  ITMGContext.GetInstance().GetPTT().CancelRecording();
                },
                label: Text("CancelRecording"),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              ElevatedButton.icon(
                icon: Icon(Icons.file_upload_outlined),
                label: Text("upload"),
                style: ElevatedButton.styleFrom(
                  minimumSize: const Size(130, 40),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5),
                  ),
                ),
                onPressed: () {
                  if (_filePath == "") {
                    showCupertinoAlertDialog(
                      "Error file",
                      "Please make a recording first",
                    );
                    return;
                  }

                  ITMGContext.GetInstance().GetPTT().UploadRecordedFile(
                    _filePath,
                  );
                },
              ),
              ElevatedButton.icon(
                icon: Icon(Icons.download_outlined),
                label: Text("download"),
                style: ElevatedButton.styleFrom(
                  minimumSize: const Size(130, 40),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5),
                  ),
                ),
                onPressed: () {
                  if (_fileId == "") {
                    showCupertinoAlertDialog(
                      "Error file",
                      "Invalid recording file, please upload the file first",
                    );
                    return;
                  }
                  ITMGContext.GetInstance().GetPTT().DownloadRecordedFile(
                    _fileId,
                    _filePath,
                  );
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              ElevatedButton.icon(
                icon: Icon(Icons.translate),
                style: ElevatedButton.styleFrom(
                  minimumSize: const Size(130, 40),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5),
                  ),
                ),
                label: Text("SpeechToText"),
                onPressed: () {
                  if (_fileId == "") {
                    showCupertinoAlertDialog(
                      "Error file",
                      "Please upload the recording file first",
                    );
                    return;
                  }
                  ITMGContext.GetInstance().GetPTT().SpeechToText(
                    _fileId,
                    strCurLanguage,
                    strCurLanguage,
                  );
                },
              ),
              Text(
                'PlayRecordedFile',
                style: Theme.of(context).textTheme.titleSmall,
              ),
              Switch(
                value: _switchPlayRecordFile,
                onChanged: (value) async {
                  if (value) {
                    int res = await ITMGContext.GetInstance()
                        .GetPTT()
                        .PlayRecordedFile(_filePath, _nVoiceType);
                    if (res != 0) {
                      showCupertinoAlertDialog(
                        "Invalid operation",
                        "Please check the error code $res",
                      );
                      return;
                    }
                  } else {
                    ITMGContext.GetInstance().GetPTT().StopPlayFile();
                  }
                  setState(() {
                    _switchPlayRecordFile = value;
                  });
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('stream', style: Theme.of(context).textTheme.titleSmall),
              Switch(
                value: _switchStreamStart,
                onChanged: (value) async {
                  if (value) {
                    if (Platform.isAndroid || Platform.operatingSystem == 'ohos') {
                      bool status = await Permission.microphone.isGranted;
                      if (!status) {
                        await Permission.microphone.request().isGranted;
                      }
                    }
                    final pathObj = await getApplicationDocumentsDirectory();
                    String filePath =
                        pathObj.path + "/record_stream" + strReordFormat;
                    int res = await ITMGContext.GetInstance()
                        .GetPTT()
                        .StartRecordingWithStreamingRecognition(
                          filePath,
                          strCurLanguage,
                          strCurLanguage,
                        );
                    if (res != 0) {
                      showCupertinoAlertDialog(
                        "Invalid operation",
                        "Please check the error code $res",
                      );
                      return;
                    }
                    _filePath = filePath;
                    micLevelTimerStart();
                  } else {
                    ITMGContext.GetInstance().GetPTT().StopRecording();
                    _micLevelTimer!.cancel();
                  }
                  setState(() {
                    _switchStreamStart = value;
                  });
                },
              ),
              DropdownButton<ITMG_VOICE_TYPE>(
                items: [
                  DropdownMenuItem(
                    child: Text(
                      "关闭特效",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_ORIGINAL_SOUND,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "幼稚园",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_KINDER_GARTEN,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "萝莉",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_LOLITA,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "大叔",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_UNCLE,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "重金属",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_HEAVY_MENTAL,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "感冒",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_INFLUENZA,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "歪果仁",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_DIALECT,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "困兽",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_CAGED_ANIMAL,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "死肥宅",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_DEAD_FATBOY,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "强电流",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_STRONG_CURRENT,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "重机器",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_HEAVY_MACHINE,
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "空灵",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: ITMG_VOICE_TYPE.ITMG_VOICE_TYPE_INTANGIBLE,
                  ),
                ],
                icon: Icon(Icons.record_voice_over_outlined),
                value: _nVoiceType,
                onChanged: (value) async {
                  int res = 0;
                  if (value != null) {
                    res = ITMGContext.GetInstance()
                        .GetAudioEffectCtrl()
                        .SetVoiceType(value);
                  }
                  if (res != 0) {
                    showCupertinoAlertDialog(
                      "Invalid operation",
                      "Please check the error code $res",
                    );
                    return;
                  }
                  setState(() {
                    if (value == null) return;
                    _nVoiceType = value;
                  });
                },
              ),
            ],
          ),
          Flex(
            direction: Axis.horizontal,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(Icons.mic_external_on),
              Slider(
                value: _micValue,
                activeColor: Colors.blueGrey,
                inactiveColor: Colors.grey,
                label: 'micValue:$_micValue',
                min: 0,
                max: 200,
                divisions: 50,
                onChangeEnd: (value) {
                  int volume = value.toInt();
                  ITMGContext.GetInstance().GetAudioCtrl().SetMicVolume(volume);
                },
                onChanged: (value) {
                  setState(() {
                    int curValue = value.toInt();
                    _micValue = curValue.toDouble();
                  });
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(Icons.speaker_outlined),
              Slider(
                value: _spkValue,
                activeColor: Colors.blueGrey,
                inactiveColor: Colors.grey,
                label: 'spkValue:$_spkValue',
                min: 0,
                max: 200,
                divisions: 50,
                onChangeEnd: (value) {
                  int volume = value.toInt();
                  ITMGContext.GetInstance().GetAudioCtrl().SetSpeakerVolume(
                    volume,
                  );
                },
                onChanged: (value) {
                  setState(() {
                    int curValue = value.toInt();
                    _spkValue = curValue.toDouble();
                  });
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              TextButton.icon(
                onPressed: () async {
                  if (_filePath != "") {
                    final int res = await ITMGContext.GetInstance()
                        .GetPTT()
                        .GetVoiceFileDuration(_filePath);
                    setState(() {
                      _fileDuration = res;
                    });
                  }
                },
                icon: Icon(Icons.file_present_rounded),
                label: Text("Lens:$_fileDuration ms"),
              ),
              //Text("$_fileDuration ms"),
              TextButton.icon(
                onPressed: () async {
                  if (_filePath != "") {
                    final int res = await ITMGContext.GetInstance()
                        .GetPTT()
                        .GetFileSize(_filePath);
                    setState(() {
                      _fileSize = res;
                    });
                  }
                },
                icon: Icon(Icons.photo_camera_front_outlined),
                label: Text("Size:$_fileSize b"),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              TextButton.icon(
                onPressed: () async {
                  if (_filePath != "") {
                    final int res =
                        await ITMGContext.GetInstance()
                            .GetAudioCtrl()
                            .GetMicVolume();
                    setState(() {
                      _micVolume = res;
                    });
                  }
                },
                icon: Icon(Icons.mic_sharp),
                label: Text("MicV:$_micVolume"),
              ),
              TextButton.icon(
                onPressed: () async {
                  if (_filePath != "") {
                    final int res =
                        await ITMGContext.GetInstance()
                            .GetAudioCtrl()
                            .GetSpeakerVolume();
                    setState(() {
                      _spkVolume = res;
                    });
                  }
                },
                icon: Icon(Icons.speaker_notes),
                label: Text("SpkV:$_spkVolume"),
              ),
            ],
          ),
          Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: []),
          Text(""),
          Listener(
            onPointerMove: (onPointerMove) {
              // 触摸收起键盘
              FocusScope.of(context).requestFocus(FocusNode());
              print('手指移动回调');
            },
            child: TextField(
              decoration: InputDecoration(
                icon: Icon(Icons.transform),
                labelText: "Texts to be translated",
                labelStyle: TextStyle(color: Colors.blueGrey, fontSize: 20),
                enabledBorder: OutlineInputBorder(
                  /*边角*/
                  borderRadius: BorderRadius.all(
                    Radius.circular(30), //边角为30
                  ),
                  borderSide: BorderSide(
                    color: Colors.blueGrey, //边线颜色为黄色
                    width: 2, //边线宽度为2
                  ),
                ),
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(
                    color: Colors.blueGrey, //边框颜色为绿色
                    width: 2, //宽度为5
                  ),
                ),
              ),
              maxLines: null,
              keyboardType: TextInputType.multiline,
              controller: _editTranslateText,
            ),
          ),
          Wrap(
            //mainAxisAlignment: MainAxisAlignment.center,
            children: [
              DropdownButton<String>(
                items: [
                  DropdownMenuItem(
                    child: Text(
                      "Mandarin Chinese",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-Hans-CN",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Taiwan Chinese",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-Hant-TW",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Cantonese",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "yue-Hant-HK",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "HongKong Chinese",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-Hans-HK",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Afrikaans",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "af-ZA",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Amharic",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "am-ET",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Armenian",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "hy-AM",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Azerbaijani",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "az-AZ",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Indonesia",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "id-ID",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Malay",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "ms-MY",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Bengali Bangladesh",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "bn-BD",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Bengali India",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "bn-IN",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Catalonia",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "ca-ES",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Czech Republic",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cs-CZ",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Danish",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "da-DK",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "German",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "de-DE",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "English Australia",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "en-AU",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "English Canada",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "en-CA",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "English Ghana",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "en-GH",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "English Britain",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "en-GB",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "English India",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "en-IN",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "English Ireland",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "en-IE",
                  ),
                ],
                icon: Icon(Icons.enhance_photo_translate_outlined),
                value: strTransLanguage,
                onChanged: (value) {
                  setState(() {
                    if (value == null) return;
                    strTransLanguage = value;
                  });
                },
              ),
              TextButton.icon(
                icon: Icon(Icons.transfer_within_a_station),
                label: Text("translate"),
                style: ElevatedButton.styleFrom(
                  minimumSize: const Size(130, 40),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5),
                  ),
                ),
                onPressed: () {
                  if (_editTranslateText.text == "") {
                    showCupertinoAlertDialog(
                      "Invalid operation",
                      "Please enter the text to be translated",
                    );
                    return;
                  }
                  ITMGContext.GetInstance().GetPTT().TranslateText(
                    _editTranslateText.text,
                    "cmn-Hans-CN",
                    strTransLanguage,
                  );
                },
              ),
            ],
          ),
          Wrap(
            //mainAxisAlignment: MainAxisAlignment.center,
            children: [
              DropdownButton<String>(
                items: [
                  DropdownMenuItem(
                    child: Text(
                      "Female Mandarin",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-CN-Standard-A",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Male Mandarin",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-CN-Standard-B",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Female Voice",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-CN-Standard-C",
                  ),
                  DropdownMenuItem(
                    child: Text(
                      "Male Voice",
                      style: Theme.of(context).textTheme.labelLarge,
                    ),
                    value: "cmn-CN-Standard-D",
                  ),
                ],
                icon: Icon(Icons.record_voice_over_sharp),
                value: strVoiceName,
                onChanged: (value) {
                  setState(() {
                    if (value == null) return;
                    strVoiceName = value;
                  });
                },
              ),
              TextButton.icon(
                onPressed: () {
                  if (_editTranslateText.text == "") {
                    showCupertinoAlertDialog(
                      "Invalid operation",
                      "Please enter the text to be translated",
                    );
                    return;
                  }

                  ITMGContext.GetInstance().GetPTT().TextToSpeech(
                    1,
                    _editTranslateText.text,
                    strVoiceName,
                    "cmn-Hans-CN",
                    1.0,
                  );
                },
                icon: Icon(Icons.speaker_group),
                label: Text('toSpeech'),
              ),
            ],
          ),

          TextField(
            controller: _editTranslateOrSpeechResult,
            decoration: const InputDecoration(
              icon: Icon(Icons.filter_1_outlined),
              labelText: 'translator or speech result',
              labelStyle: TextStyle(color: Colors.blueGrey, fontSize: 20),
              enabledBorder: OutlineInputBorder(
                /*边角*/
                borderRadius: BorderRadius.all(
                  Radius.circular(30), //边角为30
                ),
                borderSide: BorderSide(
                  color: Colors.blueGrey, //边线颜色为黄色
                  width: 2, //边线宽度为2
                ),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.blueGrey, //边框颜色为绿色
                  width: 2, //宽度为5
                ),
              ),
            ),
          ),
          Text("$strCallInfo"),
        ],
      ),
    );
  }
}
