// class DeviceLiveDetailController extends FControllerBase{
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flyinsono/managers/interfaces/device.dart';
import 'package:get/get.dart';

import '../device_live_list/controller.dart';
import 'state.dart';

/// 超声直播面板控制器
abstract class DeviceLiveDetailController extends DeviceLiveListBaseController {
  @override
  final DeviceLiveDetailState state = DeviceLiveDetailState();

  final deviceManager = Get.find<IDeviceManager>();

  /// 加载更多列表
  Future<void> loadMoreList() async {
    state.pageIndex += 1;
    await _loadList();
  }

  /// 加载后
  @protected
  Future<void> onAfterLoad() async {}

  @override
  void onReady() {
    super.onReady();
    reloadList().then((value) {
      onAfterLoad();
    });
  }

  /// 重新加载列表
  Future<void> reloadList() async {
    state.pageIndex = 1;
    await _loadList(true);
  }

  Future<void> _loadList([bool refresh = false]) async {
    busy = true;
    final pagedList = await deviceManager.getUserDevicePagedList(
      state.pageIndex,
      state.pageSize,
    );
    if (pagedList != null) {
      state.totalCount = pagedList.dataCount;
      if (refresh) {
        state.updateDeviceList(pagedList.pageData!);
        // _appendTestData(pagedList.pageData!);// TODO:
      } else {
        state.appendDeviceList(pagedList.pageData!);
      }
    }

    busy = false;
  }
}
