import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:generp/Notifiers/HomeScreenNotifier.dart';
import 'package:generp/Utils/commonServices.dart';
import 'package:generp/services/api_calling.dart';
import 'package:provider/provider.dart';

import '../../Models/commonModels/DistrictsResponse.dart';
import '../../Models/commonModels/SubLocationsResponse.dart';
import '../../Models/commonModels/commonAccountlistResponse.dart';
import '../../Models/commonModels/commonAddAccountsViewResponse.dart';

class Accountslistprovider extends ChangeNotifier {

  TextEditingController nameController = TextEditingController();
  TextEditingController mobileController = TextEditingController();
  TextEditingController addressController = TextEditingController();
  TextEditingController bankNameController = TextEditingController();
  TextEditingController branchNameController = TextEditingController();
  TextEditingController bankIfscCotroller = TextEditingController();
  TextEditingController bankHolderNameController = TextEditingController();
  TextEditingController bankAcNameController = TextEditingController();
  TextEditingController bankUpiController = TextEditingController();
  TextEditingController contactPersonController = TextEditingController();
  TextEditingController contectPersonDesignationController = TextEditingController();
  TextEditingController contectPersonAltMobController = TextEditingController();
  TextEditingController contectPersonTeleController = TextEditingController();
  TextEditingController contectPersonMailController = TextEditingController();

  TextEditingController mobileNumberController = TextEditingController();
  TextEditingController companyNameController = TextEditingController();

  List<States> _states = [];
  List<Districts> _districts = [];
  List<SubLocations> _subLocations = [];
  List<String> _accountTypes = [];
  List<AccountList> _accountsList = [];
  bool _isLoading = true;
  bool _hasMoreData = true;

  bool get isLoading => _isLoading;

  int _pageNum = 1;
  int get pageNum => _pageNum;
  set pageNum (value){
    _pageNum = value;
    notifyListeners();
  }

  bool _isVisible = false;

  List<States> get states => _states;

  List<Districts> get districts => _districts;

  List<SubLocations> get subLocations => _subLocations;

  List<String> get accountTypes => _accountTypes;
  List<AccountList> get accountsList => _accountsList;
  bool get isVisible => _isVisible;
  set isVisible (bool value){
    _isVisible = value;
    notifyListeners();
  }
  States? _selectedState;
  Districts? _selectedDistricts;
  SubLocations? _selectedSubLocations;
  String? _selectedAccountType;

  String? _selectedDistrictID;
  String? _selectedDistrictValue;

  String? _selectedStateID;
  String? _selectedStateValue;

  String? _selectedSubLocID;
  String? _selectedSubLocValue;

  States? get selectedState => _selectedState;

  String? get selectedStateID => _selectedStateID;

  String? get selectedStateValue => _selectedStateValue;

  Districts? get selectedDistricts => _selectedDistricts;

  String? get selectedDistrictId => _selectedDistrictID;

  String? get selectedDistrictValue => _selectedDistrictValue;

  SubLocations? get selectedSubLocations => _selectedSubLocations;

  String? get selectedSubLocID => _selectedSubLocID;

  String? get selectedSubLocValue => _selectedSubLocValue;

  String? get selectedAccountType => _selectedAccountType;

  set selectedAccountType(String? value) {
    _selectedAccountType = value;
    notifyListeners();
  }

  set selectedState(States? value) {
    _selectedState = value;
    _selectedStateID = value!.id;
    _selectedStateValue = value!.name;
    notifyListeners();
  }

  set selectedDistricts(Districts? value) {
    _selectedDistricts = value;
    _selectedDistrictID = value!.id;
    _selectedDistrictValue = value!.district;
    notifyListeners();
  }

  set selectedSubLocations(SubLocations? value) {
    _selectedSubLocations = value;
    _selectedSubLocID = value!.id;
    _selectedSubLocValue = value!.subLocality!;
    notifyListeners();
  }

  set selectedStateID(value){
    _selectedStateID = value;
    notifyListeners();
  }
  set selectedStateValue(value){
    _selectedStateID = value;
    notifyListeners();
  }
  set selectedDistrictId(value){
    _selectedDistrictID = value;
    notifyListeners();
  }
  set selectedDistrictValue(value){
    _selectedDistrictValue = value;
    notifyListeners();
  }

  set selectedSubLocID(value){
    _selectedSubLocID = value;
    notifyListeners();
  }

  set selectedSubLocValue(value){
    _selectedSubLocValue = value;
    notifyListeners();
  }

  Future<void> addCommonAccountViewAPI(context) async {
    try {
      var homeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.commonAddAccountViewAPI(
        homeProv.empId,
        homeProv.session,
      );
      _accountTypes.clear();
      _states.clear();
      notifyListeners();
      if (data != null) {
        if (data.error == "0") {
          _accountTypes = data.accountTypes!;
          _states = data.states!;
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

  Future<void> getDistrictAPI(context, stateID) async {
    try {
      var homeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      _districts.clear();
      notifyListeners();
      final data = await ApiCalling.commonAddAccountViewDistrictAPI(
        homeProv.empId,
        homeProv.session,
        stateID,
      );
      if (data != null) {
        if (data.error == "0") {
          _districts = data.districts!;
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

  Future<void> getSubLocationAPI(context, districtID) async {
    try {
      var homeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      _subLocations.clear();
      notifyListeners();
      final data = await ApiCalling.commonAddAccountViewSubLocationAPI(
        homeProv.empId,
        homeProv.session,
        districtID,
      );
      if (data != null) {
        if (data.error == "0") {
          _subLocations = data.subLocations!;
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

  Future<void> checkInputsAPI(context, type, value) async {
    try {
      var homeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.commonAddAccountCheckInputsAPI(
        homeProv.empId,
        homeProv.session,
        type,
        value,
      );
      if (data != null) {
        if (data.error == "0") {

        } else if (data.error == "1") {
          toast(context, data.message);
        }
      }
    } catch (e, s) {}
  }

  Future<void> submitCommonAccountsAPI(
    context,
  ) async {
    try {
      var homeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.commonAddAccountSubmitAPI(
        homeProv.empId,
        homeProv.session,
        _selectedAccountType,
        nameController.text,
        mobileController.text,
        contectPersonAltMobController.text,
        contectPersonTeleController.text,
        contectPersonMailController.text,
        contectPersonDesignationController.text,
        addressController.text,
        _selectedStateID,
        _selectedDistrictID,
        _selectedSubLocID,
        bankNameController.text,
        branchNameController.text,
        bankIfscCotroller.text,
        bankHolderNameController.text,
        bankAcNameController.text,
        bankUpiController.text,
      );
      if (data != null) {
        if (data.error == "0") {
          Navigator.pop(context);
          resetValues();
          toast(context, data.message);
        } else if (data.error == "1") {

          toast(context, data.message);
        } else {}
      }
    } catch (e, s) {}
  }




  Future<void> commonAccountListAPIFunction(context) async {
    try{
      var prov = Provider.of<HomescreenNotifier>(context,listen: false);
      if(_pageNum==1){
        _accountsList.clear();
        notifyListeners();
      }

      final data = await ApiCalling.commonAccountListAPI(prov.empId, prov.session,_pageNum,companyNameController.text,mobileNumberController.text);
      if(data!=null){
        print("pageNum $_pageNum");
        if(data.error=="0"){
            _hasMoreData = true;
            _accountsList.addAll(data.accountList!);

            _pageNum++;
            _isLoading = false;


          notifyListeners();
        }else{
          _hasMoreData = false;
          _isLoading = false;
          notifyListeners();
        }

      }

    }catch (e,s){

    }
  }

  resetValues(){
    _selectedAccountType = null;
    _selectedStateID=null;
    _selectedDistrictID=null;
    _selectedSubLocID=null;

    nameController.clear();
    mobileController.clear();
    contectPersonAltMobController.clear();
    contectPersonTeleController.clear();
    contectPersonMailController.clear();
    contectPersonDesignationController.clear();
    addressController.clear();
    bankNameController.clear();
    branchNameController.clear();
    bankIfscCotroller.clear();
    bankHolderNameController.clear();
    bankAcNameController.clear();
    bankUpiController.clear();
  }
}
