import 'dart:convert';

import 'package:siro_driver/constant/api_key.dart';
import 'package:siro_driver/constant/box_name.dart';
import 'package:siro_driver/constant/info.dart';
import 'package:siro_driver/constant/links.dart';
import 'package:siro_driver/controller/auth/captin/register_captin_controller.dart';
import 'package:siro_driver/controller/functions/crud.dart';
import 'package:siro_driver/main.dart';
import 'package:siro_driver/views/widgets/elevated_btn.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;

import '../auth/captin/login_captin_controller.dart';
import 'encrypt_decrypt.dart';

class SmsEgyptController extends GetxController {
  var headers = {'Content-Type': 'application/json'};

  Future<String> getSender() async {
    var res = await CRUD().get(link: AppLink.getSender, payload: {});
    if (res != 'failure') {
      var d = jsonDecode(res)['message'][0]['senderId'].toString();
      return d;
    } else {
      return "Sefer Egy";
    }
  }

  Future<dynamic> sendSmsEgypt(String phone) async {
    String sender = await getSender();
    var body = jsonEncode({"receiver": "2$phone"});

    var res = await http.post(
      Uri.parse(AppLink.sendSms),
      body: body,
      headers: headers,
    );

    if (jsonDecode(res.body)['message'].toString() != "Success") {
      await CRUD().post(link: AppLink.updatePhoneInvalidSMS, payload: {
        "phone_number":
            ('+2${Get.find<RegisterCaptainController>().phoneController.text}')
      });
      box.write(BoxName.phoneDriver,
          ('+2${Get.find<RegisterCaptainController>().phoneController.text}'));
      box.write(BoxName.phoneVerified, '1');

      await Get.put(LoginDriverController()).loginDriver(
        box.read(BoxName.driverID).toString(),
        (box.read(BoxName.emailDriver).toString()),
      );
    } else {
      Get.defaultDialog(
          title: 'You will receive code in sms message'.tr,
          middleText: '',
          confirm: MyElevatedButton(
              title: 'OK'.tr,
              onPressed: () {
                Get.back();
              }));
    }
  }

  Future checkCredit(String phone, otp) async {
    var res = await http.post(
      Uri.parse(AppLink.checkCredit),
      body: {
        "username": AppInformation.appName,
        "password": AK.smsPasswordEgypt,
      },
      headers: headers,
    );
  }

  Future sendSmsWithValidaty(String phone, otp) async {
    var res = await http.post(
      Uri.parse(AppLink.checkCredit),
      body: {
        "username": 'Sefer',
        "password": AK.smsPasswordEgypt,
        "message": "This is an example SMS message.",
        "language": box.read(BoxName.lang) == 'en' ? "e" : 'r',
        "sender": "Sefer", //"Kazumi", // todo add sefer sender name
        "receiver": "2$phone",
        "validity": "10",
        "StartTime": DateTime.now().toString() // "1/1/2024 10:00:00"
      },
      headers: headers,
    );
  }

  Future sendSmsStatus(String smsid) async {
    var res = await http.post(
      Uri.parse(AppLink.checkCredit),
      body: {
        "username": AppInformation.appName,
        "password": AK.smsPasswordEgypt,
        "smsid": smsid //"00b77dfc-5b8f-474d-9def-9f0158b70f98"
      },
      headers: headers,
    );
  }
}
