import 'dart:async';
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:rounded_loading_button_plus/rounded_loading_button.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../Data/Repository/Users/user_repository.dart';
import '../../../Helper/toast.dart';
import '../../../Model/Request/User/Updateuser/Updateuserrequest.dart';
import '../../../Model/Response/User/Updateuser/Updateuserresponse.dart';
import '../../../Model/Response/User/Usermodelresponse.dart';
import '../../../View/Authentication/Logiview.dart';
import '../../Authentication/Authcontroller.dart';

class EditProfileController extends GetxController{


  TextEditingController firstNameController = TextEditingController();
  TextEditingController lastNameController = TextEditingController();
  TextEditingController emailController = TextEditingController();
  TextEditingController contactNoController = TextEditingController();

  UserDetails? userInfo;

  final RoundedLoadingButtonController btnController = RoundedLoadingButtonController();

  UserRepository userRepository = UserRepository();

  AuthController authController = Get.put(AuthController());


  // Future<String?> takePhoto() async {
  //   final ImagePicker imagePicker = ImagePicker();
  //   final XFile? photo = await imagePicker.pickImage(
  //     source: ImageSource.camera,
  //     imageQuality: 50,
  //   );
  //   if (photo == null) {
  //     return null;
  //   }
  //   final imageBytes = await photo.readAsBytes();
  //   String  base64Image = base64Encode(imageBytes); // Convert image to base64
  //   print("Base64 Image: $base64Image");
  //   return base64Image;
  //
  // }


  getDetails()async{
    firstNameController.text = userInfo?.firstname??'';
    lastNameController.text = userInfo?.lastname??'';
    emailController.text = userInfo?.authname??'';
    contactNoController.text = userInfo?.contactno??'';
    // update();
  }

  void doSomething() async {
    Timer(const Duration(seconds: 1), () {
      btnController.success();
      profileValidation();
    });
  }

  profileValidation ()async{
    if (firstNameController.text.isEmpty) {
      btnController.reset();
      Toast.showToast("Please Enter Firstname");
    }
    else if (lastNameController.text.isEmpty) {
      btnController.reset();
      Toast.showToast("Please Enter Lastname");
    } else if(emailController.text.isEmpty){
      btnController.reset();
      Toast.showToast("Please Enter Email");
    }
    else {
      btnController.reset();
      userUpdate();
    }
  }

  userUpdate()async{
    userUpdateResult(
        UserUpdateRequest(
            userid:userInfo?.userid??0,
            authname:emailController.text,
            configid: userInfo?.configid??0,
            contactno:contactNoController.text,
            firstname:firstNameController.text,
            lastname : lastNameController.text,
            email:emailController.text
        )
    );
  }

  userUpdateResult(UserUpdateRequest data)async{
    UserUpdateResponse? result = await userRepository.updateUser(data);
    if(result?.status == true){
      AwesomeDialog(
        context: Get.context!,
        dialogType: DialogType.warning,
        // animType: AnimType.BOTTOMSLIDE,
        descTextStyle: const TextStyle(fontSize: 16,),
        desc: 'Update successful, Kindly login to the app for the changes to take effect',
        dismissOnTouchOutside: false,
        btnOkText: 'Logout',
        // btnCancelOnPress: () {
        //   Get.to(OrderDetails(data: data));
        // },
        btnOkOnPress: () async{
          SharedPreferences prefs = await SharedPreferences.getInstance();
          prefs.remove('token');
          prefs.remove('fcmToken');
          print('remove....fcmToken');
          prefs.clear();

          prefs.remove('userMobile');
          authController.loginPhoneNumberController.clear();
          // authController.passwordController.clear();
          Get.offAll(()=>LoginView());
        },
      ).show();

      // Get.to(()=>HomePage(selectedIndex: 3,));

    }
  }


  @override
  void onInit() {

    // TODO: implement onInit
    super.onInit();
  }

}