import { NavigationPage } from './NavigationPage';
import { ToastUtils } from '@ohos/common/src/main/ets/dialog/ToastUtils';

@Builder
export function IndexBuilder() {
  Index()
}

/**
 * 入口程序
 */
@Entry
@Component
struct Index {
  //兼容方式
  //private context = getContext(this) as common.UIAbilityContext;
  //高版本（SDK>=12）使用以下方式获取
  //private context = this.getUIContext()?.getHostContext() as common.UIAbilityContext | undefined;
  // private windowClass: window.Window | undefined = undefined;
  private lastExitTime: number = 0;

  /*
  aboutToAppear() {
    // 获取窗口实例并设置沉浸式
    window.getLastWindow(this.context).then((data: window.Window) => {
      this.windowClass = data;
      // 1. 设置全屏布局
      this.windowClass.setWindowLayoutFullScreen(true).catch(() => {

      }).then(() => {
        // 2. 隐藏状态栏和导航栏
        this.windowClass?.setWindowSystemBarEnable([]).catch(() => {
        });
      });
    });
  }

  aboutToDisappear() {
    // 页面退出时恢复系统栏
    this.windowClass?.setWindowSystemBarEnable(['status', 'navigation']).catch(() => {
    });
  }
  */

  onBackPress() {
    let currentTime = new Date().getTime();
    if (currentTime - this.lastExitTime > 2000) {
      this.lastExitTime = currentTime;
      ToastUtils.normalToast({message: '再按一次退出应用'});

      return true;
    }
    return false;
  }

  build() {
    Column() {
      NavigationPage();
    }
  }
}