import 'package:sefer_driver/views/home/on_boarding_page.dart';
import 'package:sefer_driver/views/widgets/error_snakbar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/constant/colors.dart';
import 'package:sefer_driver/constant/links.dart';
import 'package:sefer_driver/controller/functions/crud.dart';
import 'package:sefer_driver/main.dart';
import 'package:sefer_driver/views/widgets/elevated_btn.dart';
import 'package:sefer_driver/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)});
    return res['message'][0]['id'];
  }

  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: () async {
              if (checkTxtController.text == (box.read(BoxName.nameDriver))) {
                // deletecaptainAccount();

                var id = await checkBeforeDelete();
                deleteMyAccountDriver(id);
              } else {
                mySnackeBarError('Your Name is Wrong'.tr);
              }
            }));
  }

  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: () async {
                  // box.remove(BoxName.agreeTerms);
                  await box.erase();
                  await storage.deleteAll();
                  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: () async {
                  // box.remove(BoxName.agreeTerms);
                  await box.erase();
                  await storage.deleteAll();
                  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 {
        mySnackeBarError(
          'Email you inserted is Wrong.'.tr,
        );
      }
    }
  }
}
