import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/links.dart';
import 'package:SEFER/controller/functions/crud.dart';
import 'package:SEFER/main.dart';
import 'package:SEFER/onbording_page.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:SEFER/views/widgets/my_textField.dart';

import '../../constant/style.dart';

class LogOutController extends GetxController {
  TextEditingController checkTxtController = TextEditingController();
  final formKey = GlobalKey<FormState>();
  final formKey1 = GlobalKey<FormState>();
  final emailTextController = TextEditingController();

  Future deleteMyAccountDriver(String id) async {
    await CRUD().post(link: AppLink.removeUser, payload: {'id': id}).then(
        (value) => Get.snackbar('Deleted'.tr, 'Your Account is Deleted',
            backgroundColor: AppColor.redColor));
  }

  checkBeforeDelete() async {
    var res = await CRUD().post(
        link: AppLink.deletecaptainAccounr,
        payload: {'id': box.read(BoxName.driverID)}).then((value) => exit(0));
  }

  deletecaptainAccount() {
    Get.defaultDialog(
        backgroundColor: AppColor.yellowColor,
        title: 'Are you sure to delete your account?'.tr,
        middleText:
            'Your data will be erased after 2 weeks\nAnd you will can\'t return to use app after 1 month ',
        titleStyle: AppStyle.title,
        content: Column(
          children: [
            Container(
              width: Get.width,
              decoration: AppStyle.boxDecoration,
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  'Your data will be erased after 2 weeks\nAnd you will can\'t return to use app after 1 month'
                      .tr,
                  style: AppStyle.title.copyWith(color: AppColor.redColor),
                ),
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            Form(
                key: formKey,
                child: SizedBox(
                  width: Get.width,
                  child: MyTextForm(
                    controller: checkTxtController,
                    label: 'Enter Your First Name'.tr,
                    hint: 'Enter Your First Name'.tr,
                    type: TextInputType.name,
                  ),
                ))
          ],
        ),
        confirm: MyElevatedButton(
            title: 'Delete'.tr,
            onPressed: () {
              if (checkTxtController.text == box.read(BoxName.nameDriver)) {
                deletecaptainAccount();
              }
            }));
  }

  Future logOutPassenger() async {
    Get.defaultDialog(
        title: 'Are you Sure to LogOut?'.tr,
        content: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            MyElevatedButton(
              title: 'Cancel'.tr,
              onPressed: () => Get.back(),
            ),
            ElevatedButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(AppColor.redColor),
                ),
                onPressed: () {
                  // box.remove(BoxName.agreeTerms);
                  box.remove(BoxName.driverID);
                  box.remove(BoxName.email);
                  box.remove(BoxName.lang);
                  box.remove(BoxName.name);
                  box.remove(BoxName.passengerID);
                  box.remove(BoxName.phone);
                  box.remove(BoxName.tokenFCM);
                  box.remove(BoxName.tokens);
                  box.remove(BoxName.addHome);
                  box.remove(BoxName.addWork);
                  box.remove(BoxName.agreeTerms);
                  box.remove(BoxName.apiKeyRun);
                  box.remove(BoxName.countryCode);
                  box.remove(BoxName.accountIdStripeConnect);
                  box.remove(BoxName.passengerWalletTotal);
                  Get.offAll(OnBoardingPage());
                },
                child: Text(
                  'Sign Out'.tr,
                  style:
                      AppStyle.title.copyWith(color: AppColor.secondaryColor),
                ))
          ],
        ));
  }

  Future logOutCaptain() async {
    Get.defaultDialog(
        title: 'Are you Sure to LogOut?'.tr,
        titleStyle: AppStyle.title,
        content: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            MyElevatedButton(
              title: 'Cancel'.tr,
              onPressed: () => Get.back(),
            ),
            ElevatedButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(AppColor.redColor),
                ),
                onPressed: () {
                  // box.remove(BoxName.agreeTerms);
                  box.remove(BoxName.driverID);
                  box.remove(BoxName.sexDriver);
                  box.remove(BoxName.dobDriver);
                  box.remove(BoxName.nameDriver);
                  box.remove(BoxName.emailDriver);
                  box.remove(BoxName.phoneDriver);
                  box.remove(BoxName.statusDriverLocation);
                  box.remove(BoxName.cvvCodeDriver);
                  box.remove(BoxName.lastNameDriver);
                  box.remove(BoxName.passwordDriver);
                  box.remove(BoxName.cardNumberDriver);
                  box.remove(BoxName.expiryDateDriver);
                  box.remove(BoxName.cardHolderNameDriver);
                  box.remove(BoxName.vin);
                  box.remove(BoxName.make);
                  box.remove(BoxName.year);
                  box.remove(BoxName.owner);
                  box.remove(BoxName.onBoarding);
                  box.remove(BoxName.agreeTerms);
                  Get.offAll(OnBoardingPage());
                },
                child: Text(
                  'Sign Out'.tr,
                  style:
                      AppStyle.title.copyWith(color: AppColor.secondaryColor),
                ))
          ],
        ));
  }

  deletePassengerAccount() async {
    if (formKey1.currentState!.validate()) {
      if (box.read(BoxName.email).toString() == emailTextController.text) {
        await CRUD().post(link: AppLink.passengerRemovedAccountEmail, payload: {
          'email': box.read(BoxName.email),
        });
      } else {
        Get.snackbar('Email Wrong'.tr, 'Email you inserted is Wrong.'.tr,
            snackPosition: SnackPosition.BOTTOM,
            backgroundColor: AppColor.redColor);
      }
    }
  }
}
