import 'package:camera/camera.dart';
import 'package:cims_apps/application/component/select_form/select_form_view.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/data_bank/confirm_bank_account.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SubmissionDataViewModel extends ChangeNotifier {
  SubmissionDataViewModel() {
    _getData();
  }

  static int _currentStep = 1;
  int get getCurrentStep => _currentStep;
  int stepAmount = 9;
  bool _isEmailVerify = false;
  bool get isEmailVerify => _isEmailVerify;
  var formKeyPersonalData = GlobalKey<FormState>();
  var formKeySubmitEmail = GlobalKey<FormState>();
  var formKeySubmitIdCard = GlobalKey<FormState>();
  var formKeySubmitDataBank = GlobalKey<FormState>();
  TextEditingController ctrlOccupation = TextEditingController();
  TextEditingController ctrlIncome = TextEditingController();
  TextEditingController ctrlMarital = TextEditingController();
  TextEditingController ctrlSourceFund = TextEditingController();
  TextEditingController ctrlBankName = TextEditingController();
  TextEditingController ctrlNameAccountBank = TextEditingController();
  TextEditingController ctrlNoAccountBank = TextEditingController();
  TextEditingController ctrlBankNameSearch = TextEditingController();
  TextEditingController ctrlBirthDate = TextEditingController();
  int step = 1;
  String? idx;
  String valueBank = '';

  List<ItemSelectForm> listOccupation = [
    ItemSelectForm('key1', 'Student'),
    ItemSelectForm('key2', 'Entrepreneur'),
    ItemSelectForm('key3', 'Civil Servant'),
  ];
  List<ItemSelectForm> listIncome = [
    ItemSelectForm('key1', '< 10 million/year'),
    ItemSelectForm('key2', '10 – 50 million/year'),
    ItemSelectForm('key3', '50 – 100 million/year'),
  ];
  List<ItemSelectForm> listMarital = [
    ItemSelectForm('key1', 'Single'),
    ItemSelectForm('key2', 'Married'),
    ItemSelectForm('key3', 'Divorced'),
  ];
  List<ItemSelectForm> listSourceFund = [
    ItemSelectForm('key1', 'Revenue'),
    ItemSelectForm('key2', 'Business Profit'),
    ItemSelectForm('key3', 'Saving interest'),
  ];

  List<ItemSelectForm> listBank = [
    ItemSelectForm('0', 'BCA'),
    ItemSelectForm('1', 'BRI'),
    ItemSelectForm('2', 'BNI'),
    ItemSelectForm('3', 'BANK MANDIRI'),
    ItemSelectForm('4', 'CIMB NIAGA'),
    ItemSelectForm('5', 'PERMATA'),
    ItemSelectForm('6', 'BANK JATENG'),
  ];

  List<ItemSelectForm> listImg = [
    ItemSelectForm('ktp', 'ID Card', image: ''),
    ItemSelectForm('selfie', 'Selfie with ID Card', image: ''),
  ];

  Future<List<CameraDescription>> initCamera() async {
    final cameras = await availableCameras();
    final camerasDesc = cameras;
    return camerasDesc;
  }

  submitEmail() {
    _isEmailVerify = !_isEmailVerify;
    notifyListeners();
  }

  onWillPopSubmission(BuildContext context) {
    if (getCurrentStep != 1) {
      _currentStep--;
      notifyListeners();
    } else {
      Navigator.of(context).pop(true);
    }
  }

  nextSubmission(BuildContext context) {
    if (getCurrentStep < stepAmount) {
      _currentStep++;
    } else {
      //ToDo : Go To next step after completing the submission
    }

    notifyListeners();
  }

  Future<bool> next(BuildContext context) async {
    if (getCurrentStep < stepAmount) {
      _currentStep++;
      notifyListeners();
      return true;
    }
    return false;
  }

  Future<void> _getData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? imagePath = prefs.getString('imagePath');
    String? imagePathSelfie = prefs.getString('imagePathSelfie');
    if (imagePath != null && imagePathSelfie != null) {
      listImg[0].image = imagePath;
      listImg[1].image = imagePathSelfie;
    }
    notifyListeners();
  }

  changeBank(String key) {
    idx = key;
    notifyListeners();
  }

  selectBank(String value) {
    valueBank = value;
    notifyListeners();
  }

  List<ModelDataBank> listDataBank = [];

  Future<List<ModelDataBank>?> submitDataBank(
      {required String bankName, accountNumber, accountName}) async {
    List<ModelDataBank> listResponse = [
      ModelDataBank('Bank Name', bankName),
      ModelDataBank('Account Number', accountNumber),
      ModelDataBank('Account Owner Name', accountName),
      ModelDataBank('Name on ID card', accountName),
    ];
    if (listResponse.first.subtitle != null) {
      // listDataBank = listResponse;
      return listResponse;
    }
    return null;
  }
}
