import 'dart:convert';
import 'dart:typed_data';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:telnow_mobile_new/src/layouts/functions/history.dart';

class UserModule with ChangeNotifier {
  static String _serialNumber = '';
  static Map<String, dynamic> _user = {};
  static Map<String, dynamic> _profile = {};
  static bool _reset = false;
  static bool _dnd = false;
  static bool _servantDisplay = false;
  static bool _houseKeeping = false;
  static String _text = 'req_group'.tr();

  String serialNumber() => _serialNumber;
  Map<String, dynamic> user() => _user;
  Map<String, dynamic> profile() => _profile;
  bool resetData() => _reset;
  bool dndStatus() => _dnd;
  bool servantDisplay() => _servantDisplay;
  bool houseKeeping() => _houseKeeping;
  String textnya() => _text;

  void setList(list) {
    _user = list;
    notifyListeners();
  }

  void setProfile(list) {
    if(list != null){
      _profile = jsonDecode(list); 
    }
    else{
      _profile = {};
    }
    notifyListeners();
  }

  void setResetData(val) {
    _reset = val;
    notifyListeners();
  }

  void setDndStatus(val) {
    _dnd = val;
    notifyListeners();
  }

  void setHouseKeeping(val) {
    _houseKeeping = val;
    notifyListeners();
  }

  void setSerialNum(number) {
    _serialNumber = number;
    notifyListeners();
  }

  void setServantDisplay(val) {
    _servantDisplay = val;
    notifyListeners();
  }

  void setText(val) {
    _text = val;
    notifyListeners();
  }

  void reset(){
    _serialNumber = '';
    _text = 'req_group'.tr();
    _user = {};
    _profile = {};
    _reset = false;
    _servantDisplay = false;
  }
}

//------------------------------------------------------------------------------
class ReqGroupModule with ChangeNotifier {
  static List _data = [];
  static bool _isLoad = false;
  static bool _stopLoad = false;

  List data() => _data;
  bool isLoad() => _isLoad;
  bool stopLoad() => _stopLoad;

  void setData(value) {_data = value; notifyListeners();}
  void clearData() {_data.clear(); notifyListeners();}
  void setIsLoad(value) {_isLoad = value; notifyListeners();}
  void setStopLoad(value) {_stopLoad = value; notifyListeners();}
  void reset(){
    _data = [];
    _isLoad = false;
    _stopLoad = false;
  }
}

class RequestModule with ChangeNotifier {
  static List _data = [];
  static bool _isLoad = false;
  static bool _stopLoad = false;
  static bool _isEmpty = false;
  static String _placeholder = '';
  static String _keyword = '';

  List data() => _data;
  bool isLoad() => _isLoad;
  bool stopLoad() => _stopLoad;
  bool isEmpty() => _isEmpty;
  String placeholder() => _placeholder;
  String keyword() => _keyword;

  void setData(value) {_data = value; notifyListeners();}
  void clearData() {_data.clear(); notifyListeners();}
  void setIsLoad(value) {_isLoad = value; notifyListeners();}
  void setStopLoad(value) {_stopLoad = value; notifyListeners();}
  void setEmpty(value) {_isEmpty = value; notifyListeners();}
  void setPlaceholder(value) {_placeholder = value; notifyListeners();}
  void setKeyword(value) {_keyword = value; notifyListeners();}
  void reset(){
    _data = [];
    _isLoad = false;
    _stopLoad = false;
    _isEmpty = false;
    _placeholder = '';
    _keyword = '';
  }
}

class ServiceModule with ChangeNotifier {
  static String _scoopeName = '-';
  static dynamic _scoopeValue;
  static List _scoope = [];
  static bool _initialized = false;

  static List _data = [];
  static List _quickAct = [];
  static List _messages = [];
  static bool _isLoaded = false;
  static bool _stopLoad = false;
  static bool _hideQuickAct = false;
  static bool _unreadMessage = false;
  static bool _expand = false;
  static int _count = 0;

  static List? _topMenu;
  static List? _reqGroup;
  static List _offer = [];
  static List _banner = [];
  static String _contactCenter = '';

  String scoopeName() => _scoopeName;
  dynamic scoopeValue() => _scoopeValue;
  List scoope() => _scoope;
  bool initialized() => _initialized;

  List data() => _data;
  List quickAct() => _quickAct;
  List messages() => _messages;
  bool isLoaded() => _isLoaded;
  bool stopLoad() => _stopLoad;
  bool hideQuickAct() => _hideQuickAct;
  bool unreadMessage() => _unreadMessage;
  bool expand() => _expand;
  int count() => _count;
  List? topMenu() => _topMenu;
  List? reqGroup() => _reqGroup;
  List specialOffer() => _offer;
  List banner() => _banner;
  String contactCenter() => _contactCenter;

  void setInitialized(value) {
    _initialized = value;
    notifyListeners();
  }

  void setContactCenter(value) {
    _contactCenter = value;
    notifyListeners();
  }

  void setScoopeName(value) {
    _scoopeName = value;
    notifyListeners();
  }

  void setScoopeValue(value) {
    _scoopeValue = value;
    notifyListeners();
  }

  void setScoope(value) {
    _scoope = value;
    notifyListeners();
  }

  bool checkScoope(value) {
    return _scoopeValue == value;
  }

  int getScoopeLength(){
    return _scoope.length;
  }

  void setTopMenu(value) {
    _topMenu = value;
    notifyListeners();
  }

  void setReqGroup(value) {
    _reqGroup = value;
    notifyListeners();
  }

  void setSpecialOffer(value) {
    _offer = value;
    notifyListeners();
  }

  void setBanner(value) {
    _banner = value;
    notifyListeners();
  }

  void setData(value) {
    _data = value;
    notifyListeners();
  }

  int getDataLength() {
    return _data.length;
  }

  void setQuickAct(value) {
    _quickAct = value;
    notifyListeners();
  }

  void setMessage(value) {
    _messages = value;
    notifyListeners();
  }

  void setIsLoaded(value) {
    _isLoaded = value;
    notifyListeners();
  }

  void setStopLoad(value) {
    _stopLoad = value;
    notifyListeners();
  }

  void setHideQuickAct(value) {
    _hideQuickAct = value;
    notifyListeners();
  }

  void setUnreadMessage(value) {
    _unreadMessage = value;
    notifyListeners();
  }

  void setCount(value) {
    _count = value;
    notifyListeners();
  }

  void setExpand(value) {
    _expand = value;
    notifyListeners();
  }

  void reset(){
    _scoopeName = '-';
    _scoopeValue = null;
    _scoope = [];
    _initialized = false;

    _data = [];
    _quickAct = [];
    _messages = [];
    _isLoaded = false;
    _stopLoad = false;
    _hideQuickAct = false;
    _unreadMessage = false;
    _expand = false;
    _count = 0;
    _topMenu = null;
    _reqGroup = null;
    _offer = [];
    _banner = [];
  }
}

//------------------------------------------------------------------------------

class HistoryModule with ChangeNotifier {
  static int _page = 0;
  static Map<String, bool> _isLoadHistory = {};
  static bool _stopLoadHistory = false;
  static bool _multiSelectMode = false;
  static bool _setLoading = false;
  static List _selectedIndexes = [];
  static List _activeForum = [];

  static Map<String, List> _data = {};
  static List _pendingData = [];
  static String _selectedFilter = 'none';
  static String _currentPage = 'ongoing::none';
  static HistoryTab _activeTab = HistoryTab.ongoing;

  List dataRequests({String state = ''}) => _data[state != '' ? state : _currentPage]!;
  List dataPending() => _pendingData;
  String selectedFilter() => _selectedFilter;
  HistoryTab activeTab() => _activeTab;
  int page() => _page;
  bool isLoadHistory({String state = ''}) => _isLoadHistory[state != '' ? state : _currentPage]!;
  bool stopLoadHistory() => _stopLoadHistory;
  bool multiSelectMode() => _multiSelectMode;
  bool setLoading() => _setLoading;
  List selectedIndexes() => _selectedIndexes;
  List activeForum() => _activeForum;
  String currentPage() => _currentPage;

  void addSelectedIndexes(i) {
    _selectedIndexes.add(i);
    notifyListeners();
  }

  void removeSelectedIndexes(i) {
    _selectedIndexes.remove(i);
    notifyListeners();
  }

  void setSelectedIndexes(value) {
    _selectedIndexes = value;
    notifyListeners();
  }

  void setSelectedFilter(value) {
    _selectedFilter = value;
    notifyListeners();
  }

  void setActiveTab(HistoryTab value) {
    _activeTab = value;
    notifyListeners();
  }

  void setCurrentPage() {
    // debugPrint('called to set current page with value: ${_activeTab.name}::$_selectedFilter');
    _currentPage = '${_activeTab.name}::$_selectedFilter';
  }

  void setPage(value) {
    _page = value;
    notifyListeners();
  }

  void addPage() {
    _page++;
    notifyListeners();
  }

  void setLoadHistory(bool value, String state) {
    _isLoadHistory[state] = value;
    notifyListeners();
  }

  void setStopLoadHistory(value) {
    _stopLoadHistory = value;
    notifyListeners();
  }

  void setMultiSelectMode(value) {
    _multiSelectMode = value;
    notifyListeners();
  }

  void setSetLoading(value) {
    _setLoading = value;
    notifyListeners();
  }

  void setDataRequests(String keyState, List list) {
    // debugPrint('set data request => $keyState: ${list.length} data');
    _data[keyState]!.addAll(list);
    notifyListeners();
  }

  void initDataPages(Map<String, List> data){
    _data = data;
  }

  void initIsLoadPages(Map<String, bool> data){
    _isLoadHistory = data;
  }

  void clearDataRequests() {
    _data[_currentPage]!.clear();
    notifyListeners();
  }

  void setPendingData(list) {
    _pendingData = list;
    notifyListeners();
  }

  void setActiveForum(list) {
    _activeForum = list;
    notifyListeners();
  }

  bool checkSelectedFilter(value){
    return _selectedFilter == value;
  }

  int getMisiLength(){
    return _data[_currentPage]!.length;
  }

  void setSatisfaction(index, rate) {
    _data[_currentPage]![index]['satisfactionRate'] = rate;
    notifyListeners();
  }

  void cancelMisi(index) {
    _data[_currentPage]!.removeAt(index);
    notifyListeners();
  }

  void setForumFalse(index) {
    _data[_currentPage]![index]['_hasForum'] = false;
    notifyListeners();
  }

  void reset(){
    _page = 0;
    _isLoadHistory = {};
    _stopLoadHistory = false;
    _multiSelectMode = false;
    _setLoading = false;
    _data = {};
    _pendingData = [];
    _selectedFilter = 'none';
    _activeTab = HistoryTab.ongoing;
    _selectedIndexes = [];
    _activeForum = [];
  }
}

//------------------------------------------------------------------------------

class CreateSerModule with ChangeNotifier{
  int _locationType = 1;
  Uint8List? _image;
  bool _sendLater = false;
  bool _setLoadingEffect = false;
  int _others = 0;
  List _suggestion = [];
  List<Uint8List> _images = [];
  String _defaultLocation = '';

  int locationType() => _locationType;
  int others() => _others;
  Uint8List? image() => _image;
  bool sendLater() => _sendLater;
  bool setLoadingEffect() => _setLoadingEffect;
  List suggestion() => _suggestion;
  List<Uint8List> images() => _images;
  String defaultLocation() => _defaultLocation;

  void setLocationType(value) {
    _locationType = value;
    notifyListeners();
  }

  void setOthers(value) {
    _others = value;
    notifyListeners();
  }

  void setImage(value) {
    _image = value;
    notifyListeners();
  }

  void setSendLater(value) {
    _sendLater = value;
    notifyListeners();
  }

  void setSetLoadingEffect(value) {
    _setLoadingEffect = value;
    notifyListeners();
  }

  void setSuggestion(value) {
    _suggestion = value;
    notifyListeners();
  }

  void setImages(Uint8List value) {
    _images.add(value);
    notifyListeners();
  }

  void removeImages(int index) {
    _images.removeAt(index);
    notifyListeners();
  }

  void setDefaultLocation(value) {
    _defaultLocation = value;
    notifyListeners();
  }

  void reset(){
    _locationType = 1;
    _others = 0;
    _image = null;
    _images = [];
    _sendLater = false;
    _setLoadingEffect = false;
    _suggestion = [];
    _defaultLocation = '';
  }
}

//------------------------------------------------------------------------------

class MessageModule with ChangeNotifier{
  Map<String, dynamic> _user = {};
  List _data = [];
  bool _firstLoad = false;
  bool _isAfterLoad = false;
  List _forum = [];
  int _selectedTab = 0;

  Map<String, dynamic> user() => _user;
  List data() => _data;
  bool firstLoad() => _firstLoad;
  bool isAfterLoad() => _isAfterLoad;
  List forum() => _forum;
  int activeTab() => _selectedTab;

  void setUser(value) {
    _user = value;
    notifyListeners();
  }

  void setData(value) {
    _data = value;
    notifyListeners();
  }

  void setFirstLoad(value) {
    _firstLoad = value;
    notifyListeners();
  }

  void setAfterLoad(value) {
    _isAfterLoad = value;
    notifyListeners();
  }

  void reset(){
    _user = {};
    _data = [];
    _firstLoad = false;
    _isAfterLoad = false;
    _forum = [];
    _selectedTab = 0;
  }

  void setForum(val){
    _forum = val;
    notifyListeners();
  }

  void setActiveTab(val){
    // print("setActiveTab($val)");
    _selectedTab = val;
    notifyListeners();
  }
}