import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:generp/Models/crmModels/GetSegmentOnTeamResponse.dart';
import 'package:generp/Models/crmModels/GetSourceOnReferenceResponse.dart';
import 'package:generp/Models/crmModels/LeadListViewResponse.dart';
import 'package:generp/Models/crmModels/SubmitLeadListFilterResponse.dart';
import 'package:provider/provider.dart';

import '../../Models/crmModels/GetDistrictOnStateResponse.dart';
import '../../Models/crmModels/GetSubLocOnDistrictResponse.dart';
import '../../Models/crmModels/LeadListContactPopUpResponse.dart' show Contacts;
import '../../services/api_calling.dart';
import '../HomeScreenNotifier.dart';

class Leadlistprovider extends ChangeNotifier {
  TextEditingController sLeadIDController = TextEditingController();
  TextEditingController mobileNumberController = TextEditingController();
  TextEditingController companyNameController = TextEditingController(
    text: "a",
  );

  bool _isLoading = true;

  List<Sources> _sourcesList = [];
  List<Teams> _teamsList = [];
  List<States> _statesList = [];
  List<Employees> _employeesList = [];
  List<References> _referencesList = [];
  List<Segments> _segmentsList = [];
  List<Districts> _districtsList = [];
  List<SubLocations> _subLocationsList = [];
  List<Contacts> _contactsList = [];
  List<LeadList> _crmLeadList = [];
  List<String?> _leadStatusList = [];
  List<String?> _openStatusList = [];

  Sources? _selectedSources;
  Teams? _selectedTeams;
  States? _selectedStates;
  Employees? _selectedEmployees;
  References? _selectedReferences;
  Segments? _selectedSegments;
  Districts? _selectedDistricts;
  SubLocations? _selectedSubLocations;
  Contacts? _selectedContacts;
  String? _selectedLeadStatus;
  String? _selectedOpenStatus;

  String? _selectedSourceId;
  String? _selectedSourceValue;

  String? _selectedTeamId;
  String? _selectedTeamValue;

  String? _selectedStateId;
  String? _selectedStateValue;

  String? _selectedEmployeeId;
  String? _selectedEmployeeValue;

  String? _selectedReferenceId;
  String? _selectedReferenceValue;

  String? _selectedSegmentId;
  String? _selectedSegmentValue;

  String? _selectedDistrictId;
  String? _selectedDistrictValue;

  String? _selectedSubLocationId;
  String? _selectedSubLocationValue;

  String? _selectedContactId;
  String? _selectedContactValue;

  bool get isLoading => _isLoading;

  List<Sources> get sourcesList => _sourcesList;

  List<Teams> get teamsList => _teamsList;

  List<States> get statesList => _statesList;

  List<Employees> get employeesList => _employeesList;

  List<References> get referencesList => _referencesList;

  List<Segments> get segmentsList => _segmentsList;

  List<Districts> get districtsList => _districtsList;

  List<SubLocations> get subLocationsList => _subLocationsList;

  List<Contacts> get contactsList => _contactsList;

  List<LeadList> get crmLeadList => _crmLeadList;

  List<String?> get leadStatusList => _leadStatusList;

  List<String?> get openStatusList => _openStatusList;

  Sources? get selectedSources => _selectedSources;

  Teams? get selectedTeams => _selectedTeams;

  States? get selectedStates => _selectedStates;

  Employees? get selectedEmployees => _selectedEmployees;

  References? get selectedReferences => _selectedReferences;

  Segments? get selectedSegments => _selectedSegments;

  Districts? get selectedDistricts => _selectedDistricts;

  SubLocations? get selectedSubLocations => _selectedSubLocations;

  Contacts? get selectedContacts => _selectedContacts;

  String? get selectedLeadStatus => _selectedLeadStatus;

  String? get selectedOpenStatus => _selectedOpenStatus;

  String? get selectedSourceId => _selectedSourceId;

  String? get selectedSourceValue => _selectedSourceValue;

  String? get selectedTeamId => _selectedTeamId;

  String? get selectedTeamValue => _selectedTeamValue;

  String? get selectedStateId => _selectedStateId;

  String? get selectedStateValue => _selectedStateValue;

  String? get selectedEmployeeId => _selectedEmployeeId;

  String? get selectedEmployeeValue => _selectedEmployeeValue;

  String? get selectedReferenceId => _selectedReferenceId;

  String? get selectedReferenceValue => _selectedReferenceValue;

  String? get selectedSegmentId => _selectedSegmentId;

  String? get selectedSegmentValue => _selectedSegmentValue;

  String? get selectedDistrictId => _selectedDistrictId;

  String? get selectedDistrictValue => _selectedDistrictValue;

  String? get selectedSubLocationId => _selectedSubLocationId;

  String? get selectedSubLocationValue => _selectedSubLocationValue;

  String? get selectedContactId => _selectedContactId;

  String? get selectedContactValue => _selectedContactValue;

  set isLoading(bool value) {
    _isLoading = value;
    notifyListeners();
  }

  set selectedSources(Sources? value) {
    _selectedSources = value;
    _selectedSourceId = value!.id!;
    _selectedSourceValue = value!.name!;
    notifyListeners();
  }

  set selectedTeams(Teams? value) {
    _selectedTeams = value;
    _selectedTeamId = value!.id!;
    _selectedTeamValue = value.name;
    notifyListeners();
  }

  set selectedStates(States? value) {
    _selectedStates = value;
    _selectedStateId = value!.id!;
    _selectedStateValue = value.name;
    notifyListeners();
  }

  set selectedEmployees(Employees? value) {
    _selectedEmployees = value;
    _selectedEmployeeId = value!.id!;
    _selectedEmployeeValue = value.name;
    notifyListeners();
  }

  set selectedReferences(References? value) {
    _selectedReferences = value;
    _selectedReferenceId = value!.id!;
    _selectedReferenceValue = value.name;
    notifyListeners();
  }

  set selectedSegments(Segments? value) {
    _selectedSegments = value;
    _selectedSegmentId = value!.id!;
    _selectedSegmentValue = value.name;
    notifyListeners();
  }

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

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

  set selectedContacts(Contacts? value) {
    _selectedContacts = value;
    _selectedContactId = value!.id!;
    _selectedContactValue = value.name;
    notifyListeners();
  }

  set selectedLeadStatus(String? value) {
    _selectedLeadStatus = value;
    notifyListeners();
  }

  set selectedOpenStatus(String? value) {
    _selectedOpenStatus = value;
    notifyListeners();
  }

  set selectedSourceId(String? value) {
    _selectedSourceId = value;
    notifyListeners();
  }

  set selectedSourceValue(String? value) {
    _selectedSourceValue = value;
    notifyListeners();
  }

  set selectedTeamId(String? value) {
    _selectedTeamId = value;
    notifyListeners();
  }

  set selectedTeamValue(String? value) {
    _selectedTeamValue = value;
    notifyListeners();
  }

  set selectedStateId(String? value) {
    _selectedStateId = value;
    notifyListeners();
  }

  set selectedStateValue(String? value) {
    _selectedStateValue = value;
    notifyListeners();
  }

  set selectedEmployeeId(String? value) {
    _selectedEmployeeId = value;
    notifyListeners();
  }

  set selectedEmployeeValue(String? value) {
    _selectedEmployeeValue = value;
    notifyListeners();
  }

  set selectedReferenceId(String? value) {
    _selectedReferenceId = value;
    notifyListeners();
  }

  set selectedReferenceValue(String? value) {
    _selectedReferenceValue = value;
    notifyListeners();
  }

  set selectedSegmentId(String? value) {
    _selectedSegmentId = value;
    notifyListeners();
  }

  set selectedSegmentValue(String? value) {
    _selectedSegmentValue = value;
    notifyListeners();
  }

  set selectedDistrictId(String? value) {
    _selectedDistrictId = value;
    notifyListeners();
  }

  set selectedDistrictValue(String? value) {
    _selectedDistrictValue = value;
    notifyListeners();
  }

  set selectedSubLocationId(String? value) {
    _selectedSubLocationId = value;
    notifyListeners();
  }

  set selectedSubLocationValue(String? value) {
    _selectedSubLocationValue = value;
    notifyListeners();
  }

  set selectedContactId(String? value) {
    _selectedContactId = value;
    notifyListeners();
  }

  set selectedContactValue(String? value) {
    _selectedContactValue = value;
    notifyListeners();
  }

  Future<void> crmLeadListViewAPIFunction(context, mode) async {
    try {
      var HomeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadListViewAPI(
        HomeProv.empId,
        HomeProv.session,
        mode,
      );
      if (data != null) {
        print(data.error);
        if (data.error == "0") {
          // print("as");
          _leadStatusList = [
            "all",
            "Hot",
            "Warm",
            "Cold",
            "Order Gain",
            "Order Lost",
          ];
          _openStatusList = ["open", "Closed", "All"];
          _sourcesList = data.sources!;
          _teamsList = data.teams!;
          _statesList = data.states!;
          _employeesList = data.employees!;
          checkDropDownValues();
          // print(_leadStatusList);
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

  Future<void> crmLeadListSourceOnReferenceAPIFunction(
    context,
    mode,
    sourceID,
  ) async {
    try {
      var prov = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadListSourceOnReferenceAPI(
        prov.empId,
        prov.session,
        sourceID,
      );
      if (data != null) {
        if (data.error == "0") {
          _referencesList = data.references!;
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

  Future<void> crmLeadListSegmentOnTeamAPIFunction(
    context,
    mode,
    teamID,
  ) async {
    try {
      var prov = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadListSegmentOnTeamAPI(
        prov.empId,
        prov.session,
        teamID,
      );
      if (data != null) {
        if (data.error == "0") {
          _segmentsList = data.segments!;
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

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

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

  Future<void> crmLeadListContactPopUpAPIFunction(
    context,
    mode,
    accountID,
  ) async {
    try {
      var prov = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadListContactPopUpAPI(
        prov.empId,
        prov.session,
        accountID,
      );
      if (data != null) {
        if (data.error == "0") {
          _contactsList = data.contacts!;
          notifyListeners();
        }
      }
    } catch (e, s) {}
  }

  Future<void> crmLeadListAPIFunction(
    context,
    mode,
    employeeID,
    leadStatus,
    openStatus,
    sourceID,
    referenceID,
    teamID,
    segmentID,
    stateID,
    districtID,
    subLocID,
  ) async {
    try {
      _crmLeadList.clear();
      notifyListeners();
      var HomeProv = Provider.of<HomescreenNotifier>(context, listen: false);
      final data = await ApiCalling.crmLeadListFilterSubmitAPI(
        HomeProv.empId,
        HomeProv.session,
        mode,
        employeeID,
        sLeadIDController.text,
        leadStatus,
        openStatus,
        mobileNumberController.text,
        companyNameController.text,
        sourceID,
        referenceID,
        teamID,
        segmentID,
        stateID,
        districtID,
        subLocID,
      );
      if (data != null) {
        _isLoading = true;
        notifyListeners();
        if (data.error == "0") {
          _crmLeadList = data.leadList!;
          _isLoading = false;
          checkDropDownValues();
          notifyListeners();
        } else {
          _isLoading = false;
          notifyListeners();
        }
      } else {
        _isLoading = false;
        notifyListeners();
      }
    } catch (e, s) {
      _isLoading = false;
      notifyListeners();
    }
  }

  onChangedLeadId(value) {
    notifyListeners();
  }

  onChangedMobileNum(value) {
    notifyListeners();
  }

  onChangedCompanyName(value) {
    notifyListeners();
  }

  void resetForm() {
    _isLoading = false;
    sLeadIDController.clear();
    mobileNumberController.clear();
    companyNameController.clear();
    companyNameController.text = "a";
    _selectedEmployees = null;
    _selectedSources = null;
    _selectedReferences = null;
    _selectedSegments = null;
    _selectedTeams = null;
    _selectedDistricts = null;
    _selectedStates = null;
    _selectedSubLocations = null;
    _selectedLeadStatus = null;
    _selectedOpenStatus = null;
    _selectedEmployeeId = null;
    _selectedSourceId = null;
    _selectedReferenceId = null;
    _selectedTeamId = null;
    _selectedSegmentId = null;
    _selectedStateId = null;
    _selectedDistrictId = null;
    _selectedSubLocationId = null;

    _selectedEmployeeValue = null;
    _selectedSourceValue = null;
    _selectedReferenceValue = null;
    _selectedTeamValue = null;
    _selectedSegmentValue = null;
    _selectedDistrictValue = null;
    _selectedStateValue = null;
    _selectedSubLocationValue = null;
    checkDropDownValues();
    notifyListeners();
  }

  void checkDropDownValues() {
    if (!_employeesList.contains(_selectedEmployees) &&
        _selectedEmployees != null) {
      _selectedEmployeeId = null;
      _selectedEmployeeValue = null;
    }
    if (!_sourcesList.contains(_selectedSources) && _selectedSources != null) {
      _selectedSourceId = null;
      _selectedEmployeeValue = null;
    }
    if (!_referencesList.contains(_selectedReferences) &&
        _selectedReferences != null) {
      _selectedReferenceId = null;
      _selectedSourceValue = null;
    }
    if (!_segmentsList.contains(_selectedSegments) &&
        _selectedSegments != null) {
      _selectedTeamId = null;
      _selectedReferenceValue = null;
    }
    if (!_teamsList.contains(_selectedTeams) && _selectedTeams != null) {
      _selectedSegmentId = null;
      _selectedTeamValue = null;
    }
    if (!_districtsList.contains(_selectedDistricts) &&
        _selectedDistricts != null) {
      _selectedDistrictId = null;
      _selectedSegmentValue = null;
    }
    if (!_statesList.contains(_selectedStates) && _selectedStates != null) {
      _selectedStateId = null;
      _selectedDistrictValue = null;
    }
    if (!_subLocationsList.contains(_selectedSubLocations) &&
        _selectedSubLocations != null) {
      _selectedSubLocationId = null;
      _selectedStateValue = null;
    }
    notifyListeners();
  }
}
