import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:open_file/open_file.dart';
import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
import 'package:telnow_mobile_new/src/api/jwt_token.dart';
import 'package:telnow_mobile_new/src/layouts/components/template.dart';
import 'package:telnow_mobile_new/src/utils/U.dart';
import 'package:translator/translator.dart';

class DetailFunction{
  final ApiAuthProvider apiAuthProvider = ApiAuthProvider();
  final JwtToken token = JwtToken();
  final translator = GoogleTranslator();

  Future getMission(BuildContext context, list) async{
    var userData = await token.getUserData(context);
    var data = list;
    if (data['datetimeScheduled'] != null && data['datetimeScheduled'] != '' && data['noteFormat'] == 'DATE') {
      var date = data['datetimeScheduled'];
      data['datetimeScheduled'] = DateFormat('dd MMM yyyy HH:mm', 'id').format(DateTime.parse(date));
    }

    data['noteStartTranslate'] = '';
    data['noteFinishTranslate'] = '';
    data['noteCompleteTranslate'] = '';
    data['noteCancelTranslate'] = '';

    var locale = context.locale.toString() == 'zh' ? 'zh-cn' : context.locale.toString();

    if (data['noteStart'] == 'transferNoteBySystem'){
      data['noteStart'] = 'transferNoteBySystem'.tr();
    } else if (data['noteStart'] == 'escalationNoteBySystem'){
      data['noteStart'] = 'escalationNoteBySystem'.tr();
    } else{
      if(U.autoTranslate() && data['noteStart'] != null && data['noteStart'] != ''){
        var translate = await translator.translate(data['noteStart'], to: locale);
        data['noteStartTranslate'] = translate.text;
      }
    }

    if(U.autoTranslate() && data['noteFinish'] != null && data['noteFinish'] != ''){
      var translate = await translator.translate(data['noteFinish'], to: locale);
      data['noteFinishTranslate'] = translate.text;
    }
    if(U.autoTranslate() && data['noteComplete'] != null && data['noteComplete'] != ''){
      var translate = await translator.translate(data['noteComplete'], to: locale);
      data['noteCompleteTranslate'] = translate.text;
    }
    if(U.autoTranslate() && data['noteCancel'] != null && data['noteCancel'] != ''){
      var translate = await translator.translate(data['noteCancel'], to: locale);
      data['noteCancelTranslate'] = translate.text;
    }
    data['_collaboratorDataFilter'] = data['_collaboratorData'].where((el) => el['status'] == 'ACCEPTED').toList();

    return {
      'user': userData,
      'list': data
    };
  }

  openAttachment(list) async{
    var filePath = await token.getPath()+'/TelNow/Files/';
    if(!File(filePath+getFileName(list['responseAttachment'])).existsSync()){
      // print("file isnot ready yet");
      await apiAuthProvider.downloadImage(list['responseAttachment'], filePath+getFileName(list['responseAttachment']));
    }
    await OpenFile.open(filePath+getFileName(list['responseAttachment']));
  }

  getFileName(pdfUrl){
    var imgSplit = pdfUrl.toString().split('/');
    return imgSplit[imgSplit.length-1];
  }

  cancelRequest(BuildContext context, list, controllerNote){
    var note = controllerNote.text.trim();
    if (note.length > 128) {
      showError(context, 'noteLongAlert'.tr());
    }
    else{
      dialogConfirm(context: context, title: 'buttonCancel'.tr(), text: 'confirmCancel'.tr(), actionYes: ()async{
        showLoading(context);
        var data = {
          'note': controllerNote.text.trim(),
        };

        var res = await apiAuthProvider.postData('/api/requestHistories/search/request/cancel/'+list['ticketNo'], null, data, context);
        if(res != null){
          closeLoading(context);
          navigateBack(context, exc: true);
        }
        else{
          closeLoading(context);
        }
      });
    }
  }
}