import 'package:cashier_reserve/common/base/ui.dart';
import 'package:cashier_reserve/common/base/ui_model.dart';
import 'package:cashier_reserve/common/channel/call_log_model.dart';
import 'package:cashier_reserve/common/channel/channel_manager.dart';
import 'package:cashier_reserve/common/manager/event_manager.dart';
import 'package:cashier_reserve/data_model/reserve/table_area_model.dart';
import 'package:cashier_reserve/data_model/reserve/table_model.dart';
import 'package:cashier_reserve/model/reserve_model.dart';

class ReserveViewModel extends BaseUIModel  {
  Map<int, String> weekdayMap = {
    1: "星期一",
    2: "星期二",
    3: "星期三",
    4: "星期四",
    5: "星期五",
    6: "星期六",
    7: "星期日",
  };

  Map<int, String> dayInfoMap = {
    0: "今",
    1: "明",
    2: "后",
  };

  PageController pageController = PageController();

  DateTime now = DateTime.now();

  int selectedDateIndex = 0;
  String selectedDate = "";
  String bookingType = "lunch";

  List<TableAreaModel?>? _tableAreaList;
  List<TableAreaModel?>? get tableAreaList => _tableAreaList;

  List<TableModel?>? _tableList;
  List<TableModel?>? get tableList => _tableList;
  Map<String, List<TableModel?>> tableMap = {};

  List<CallLogModel?>? callLogs = [];
  
  ReserveViewModel() {
    selectedDate = "${now.year}-${now.month}-${now.day}";

    EventManager.addListener<GetCallLogEvent>(this, (event) {
      if (event.isSuccess) {
        if (!event.isSuccess) {
          return;
        }
        callLogs = event.callLogs;
        notifyListeners();
      }
    });

    loadCallLog();
    loadTableAreaList();
    loadAreaTableList(0);
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }


  void loadCallLog() {
    ChannelManager.getCallLog("getCallLog");
  }

  void loadTableAreaList() async {
    final r = await ReserveModel.getShopTableAreaList();

    _tableAreaList = r;

    _tableAreaList ??= [];

    _tableAreaList!.insert(0, TableAreaModel(id: 0, name: "全部"));

    notifyListeners();
  }

  void loadAreaTableList(int areaId) async {
    final r = await ReserveModel.getAreaTableList(areaId, selectedDate, bookingType);

    _tableList = r;

    _tableList ??= [];

    tableMap = {};
    tableMap["0"] = _tableList!;
    for (var item in _tableList!) {
      String areaId = item!.areaId.toString();
      if (tableMap[areaId] == null) {
        tableMap[areaId] = [];
      }
      tableMap[areaId]!.add(item);
    }

    notifyListeners();
  }

  void setSelectedDateIndex(int index) {
    selectedDateIndex = index;

    DateTime offsetDay = now.add(Duration(days: index ~/ 2));
    selectedDate = "${offsetDay.year}-${offsetDay.month}-${offsetDay.day}";
    bookingType = index % 2 == 0 ? "lunch" : "dinner";

    notifyListeners();

    loadAreaTableList(0);
  }

  String getCurrentDate() {
    return "${now.year}/${now.month}/${now.day}";
  }

  String getOffsetDay(int offset) {
    DateTime offsetDay = now.add(Duration(days: offset));
    return "${dayInfoMap[offset]}/${offsetDay.day}";
  }

  String getOffsetWeekday(int offset) {
    DateTime offsetDay = now.add(Duration(days: offset));
    return weekdayMap[offsetDay.weekday] ?? "";
  }
}