// import 'package:camera/camera.dart';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:uco_mobile_poc/app/core/config/server_response.dart';
import 'package:uco_mobile_poc/app/core/constants/translation_keys.dart';
import 'package:uco_mobile_poc/app/core/data/repositories/app_repositories.dart';
import 'package:uco_mobile_poc/app/core/utils/logs_utils.dart';
import 'package:uco_mobile_poc/app/custom_widgets/custom_dialog.dart';
import 'package:uco_mobile_poc/app/custom_widgets/custom_dropdown.dart';
import 'package:uco_mobile_poc/app/custom_widgets/custom_toasty.dart';
import 'package:uco_mobile_poc/app/models/SignupStep3RequestModel.dart';
import 'package:uco_mobile_poc/app/modules/sign_up/controllers/sign_up_screen_step1_controller.dart';
import 'package:uco_mobile_poc/app/modules/sign_up/controllers/sign_up_screen_step2_controller.dart';
import 'package:uco_mobile_poc/app/routes/app_pages.dart';
import '../../../core/constants/app_contants.dart';
import '../../../core/constants/form_field_constants.dart';
import '../../../core/utils/fields_utils.dart';
import 'sign_up_screen_otp_controller.dart';

enum signDocTypeScreen { cnic, passport, driving }

class SignUpScreenStep3Controller extends GetxController {
  BuildContext context = Get.context as BuildContext;
  final AppRepositories repository = Get.find<AppRepositories>();
  RxList<DropDown> documentTypeTabList = <DropDown>[].obs;
  RxString frontPath = "".obs;
  RxString backPath = "".obs;

  Rx<signDocTypeScreen> docTypeScreen = signDocTypeScreen.cnic.obs;
  SignupStep3RequestModel signupStep3RequestModel = SignupStep3RequestModel.empty();

  @override
  Future<void> onReady() async {
    super.onReady();
    var arguments = Get.arguments;
    signupStep3RequestModel = (arguments != null ? arguments[AppConstants.SIGN_UP_SECOND_STEP_DATA] ?? SignupStep3RequestModel.empty() : SignupStep3RequestModel.empty());
  }

  updateDocTypeScreen(signDocTypeScreen type) {
    if (type != docTypeScreen.value) {
      frontPath.value = "";
      backPath.value = "";
      docTypeScreen.value = type;
    }
  }

  updateFrontPath(path) {
    frontPath.value = path;
  }

  updateBackPath(path) {
    backPath.value = path;
  }

  void updateDocs(String path, bool isFront, signDocTypeScreen docType) {
    if (isFront) {
      updateFrontPath(path);
    } else {
      updateBackPath(path);
    }
  }

  String getKycType() {
    if (docTypeScreen == signDocTypeScreen.cnic) {
      return AppConstants.IDEN_TYPE_CNIC;
    } else if (docTypeScreen == signDocTypeScreen.passport) {
      return AppConstants.IDEN_TYPE_PASSPORT;
    } else if (docTypeScreen == signDocTypeScreen.driving) {
      return AppConstants.IDEN_TYPE_DRIVING_LICIENCE;
    }
    return "";
  }

  void skipAndSubmit() {
    signupStep3RequestModel.kycType = "";
    signupStep3RequestModel.isKycAdded = false;
    signupStep3RequestModel.kycDocumentId1 = "";
    signupStep3RequestModel.kycDocumentId2 = "";
    sendRequest();
  }

  void validateAndSubmit() {
    hideKeyBoard();
    if (frontPath.value.isEmpty) {
      Toasty.error("Please upload front image");
      return;
    } else if (docTypeScreen.value != signDocTypeScreen.passport) {
      if (backPath.value.isEmpty) {
        Toasty.error("Please upload back image");
        return;
      }
    }

    signupStep3RequestModel.kycType = getKycType();
    signupStep3RequestModel.isKycAdded = true;
    signupStep3RequestModel.kycDocumentId1 = frontPath.value;
    signupStep3RequestModel.kycDocumentId2 = backPath.value;
    sendRequest();
  }

  Future<void> sendRequest() async {
    dp("=======sendRequest>>>>>>>", signupStep3RequestModel.toString());
    dp("=======sendRequest>>>>>>>", signupStep3RequestModel.porOrgacode + "Rehan");
    signupStep3RequestModel.porOrgacode = "1962";
    signupStep3RequestModel.plcLocacode= "0010";
    dp("=======sendRequest>>>>>>>", signupStep3RequestModel.porOrgacode + "Rehan");
    ServerResponse response = await repository.signupStep3(signupStep3RequestModel);
    if (response.isError) {
      Toasty.error(response.errorMsg);
      return;
    }
    CustomDialog.showSuccessDialog(
        description: TranslationKeys.makeTranslation(TranslationKeys.textSignUpDescriptionDailog),
        onTapPositive: () {
          Get.back();
          // Get.offAndToNamed(Routes.LOGIN_SCREEN);
          // step1Controller.clearTextControllerStep1();
          // otpController.clearControllersOTP();
          // step2Controller.clearTextControllerStep2();
          Get.delete<SignUpScreenStep1Controller>();
          Get.delete<SignUpScreenOTPController>();
          Get.delete<SignUpScreenStep2Controller>();
          Get.offAllNamed(Routes.LOGIN_SCREEN);
        });
  }

  var step1Controller = Get.find<SignUpScreenStep1Controller>();
  var otpController = Get.find<SignUpScreenOTPController>();
  var step2Controller = Get.find<SignUpScreenStep2Controller>();

  /// Flash Light work
  late CameraController _controller;
  late List<CameraDescription> cameras;
  var isFlashOn = false.obs;

  @override
  void onInit() {
    super.onInit();
    initializeCamera();
  }

  Future<void> initializeCamera() async {
    cameras = await availableCameras();
    _controller = CameraController(cameras[0], ResolutionPreset.low, enableAudio: false);
    await _controller.initialize();

    // Restore flash state if it was previously on
    if (_controller.value.flashMode == FlashMode.torch) {
      isFlashOn(true);
    }
  }

  Future<void> toggleFlashlight() async {
    if (_controller.value.isInitialized) {
      if (_controller.value.flashMode == FlashMode.off) {
        await _controller.setFlashMode(FlashMode.torch);
        isFlashOn(true);
      } else {
        await _controller.setFlashMode(FlashMode.off);
        isFlashOn(false);
      }
    }
  }
  @override
  void onClose() {
    FormFieldConstants.instance().clearKeysForScreen("SignUpScreenStep3View");
    _controller.dispose();
    super.onClose();
  }
}
