import { getFocusedRouteNameFromRoute } from "@react-navigation/core";
import { PagerList } from "./ChargeAdapter";
import app from '../../../app.json';

var chargeInfoState = global.chargeInfoState
var refreshListener = [];

const DEBUG = app.debug && !app.product;

export default PagerUtil = {
  ENABLE_NEW_UI: false, //是否启用新的充电页面
  getStationInfo: () => {
    return chargeInfoState.stationInfo ?? {}
  },
  setStationInfo: (info) => {
    chargeInfoState.stationInfo = info;
  },
  addOnRefresh: (page) => {
    refreshListener.push(page)
  },
  setRefreshing: (route) => {
    if (DEBUG) {
      console.log("[PagerUtil]刷新指定子页面：", route);
    }
    refreshListener.map((item, index) => {
      if (!route || route == item.props?.route?.name) {
        if (item.onRefresh)
          item.onRefresh();
      }
    })
  },
  setBackRefreshing: () => {
    refreshListener.map((item, index) => {
      if (item.onBackRefresh)
        item.onBackRefresh();
    })
  },
  onCharge: (props) => {
    const isFocused = props?.navigation?.isFocused();
    if (!isFocused) {
      if (DEBUG) {
        console.log("[PagerUtil]", "onCharge");
      }
      startPage(PagerList.tabCharge);
    } else if (DEBUG) {
      console.log("[PagerUtil]", "onCharge-skip");
    }
  },
  onReserve: (props) => {
    const isFocused = props?.navigation?.isFocused();
    if (!isFocused) {
      if (DEBUG) {
        console.log("[PagerUtil]", "onReserve");
      }
      startPage(PagerList.tabReserve);
    } else if (DEBUG) {
      console.log("[PagerUtil]", "onReserve-skip");
    }
  },
  onEnterStation: (props) => {
    const isFocused = props?.navigation?.isFocused();
    if (!isFocused) {
      if (DEBUG) {
        console.log("[PagerUtil]", "onEnterStation");
      }
      startPage(PagerList.tabCharge);
    } else if (DEBUG) {
      console.log("[PagerUtil]", "onEnterStation-skip");
    }
  },
  onDestory: () => {
    chargeInfoState = {};
    refreshListener = [];
  },
  getCurrentRoute: (props) => (
    getFocusedRouteNameFromRoute(props.route)
  )
}