import 'package:cashier_reserve/call/call_view.dart';
import 'package:cashier_reserve/common/base/ui.dart';
import 'package:cashier_reserve/common/base/ui_model.dart';
import 'package:cashier_reserve/common/channel/channel_manager.dart';
import 'package:cashier_reserve/common/channel/model/call_status_change_model.dart';
import 'package:cashier_reserve/common/manager/app_manager.dart';
import 'package:cashier_reserve/common/manager/event_manager.dart';
import 'package:cashier_reserve/common/print/print.dart';
import 'package:cashier_reserve/common/push/push.dart';

class HomeViewModel extends BaseUIModel {
  int _currentIndex = 0;

  int get currentIndex => _currentIndex;

  final List<String> _tabTitles = [
    "预定",
    "订单",
    "打印预定",
    "历史订单",
    "来电",
    "客户",
    "消息",
    "更多"
  ];
  final List<String> _tabIcons = [
    "reserve",
    "order",
    "print",
    "history",
    "tel",
    "customer",
    "message",
    "more"
  ];

  List<String> get tabTitles => _tabTitles;

  List<String> get tabIcons => _tabIcons;

  PageController? _pageController;

  PageController? get pageController => _pageController;

  bool isShowCallView = false;

  HomeViewModel() {
    _pageController = PageController(initialPage: 0);

    Future.delayed(const Duration(milliseconds: 700), () {
      _checkLogin();
    });

    EventManager.addListener<CallStatusChangeEvent>(this, (event) {
      yjPrint(
          "HomeViewModel CallStatusChangeEvent state: ${event.model.state}");

      /// state = IncomingNumberReceived 来电接听
      /// state = OutGoing 呼出
      /// state = End 通话结束
      /// state = Incoming 来电
      /// state = Idle 空闲

      switch (event.model.state) {
        case "IncomingNumberReceived":
          EventManager.postEvent(CallReceivedEvent());
          break;
        case "OutGoing":
          break;
        case "Incoming":
          showCallInfoView(event.model);
          break;
        default:
          hideCallInfoView();
          break;
      }
    });
  }

  @override
  void dispose() {
    _pageController?.dispose();
    super.dispose();
  }

  _checkLogin() {
    bool flag = AppManager.isLogin();
    yjPrint("is login $flag");
  }

  void setIndex(int index) {
    if (_currentIndex == index) {
      return;
    }
    _currentIndex = index;
    notifyListeners();

    _pageController?.jumpToPage(index);
  }

  showCallInfoView(CallStatusChangeModel model) {
    if (isShowCallView) {
      return;
    }
    YJPush.presentWidget(
        context!,
        CallView(
            statusModel: model,
            onAction: (action) {
              yjPrint("call view action: $action");
              if (action == "accept") {
                ChannelManager.acceptCall();
              } else {
                ChannelManager.rejectCall();
                hideCallInfoView();
              }
            }));
    isShowCallView = true;
  }

  hideCallInfoView() {
    if (!isShowCallView) {
      return;
    }
    Navigator.of(context!).pop();
    isShowCallView = false;

    ChannelManager.getCallLog("getCallLog");
  }
}
