
// import 'package:riverpod_annotation/riverpod_annotation.dart';

// part 'notice_board_view_model.g.dart';

// @riverpod
// class NoticeBoardViewModel extends _$NoticeBoardViewModel {

//   @override
//   void build(){

//   }

// }


import 'package:cs_resources/generated/assets.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:flutter/cupertino.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';

// import '../../ioan/property_ioan_page.dart';
import '../../announ/page/announ_page.dart';
import '../../documents/page/documents_page.dart';
import '../../event/page/event_page.dart';

part 'notice_board_view_model.g.dart';


class NoticeBoardVmState {
  List<Map<String, dynamic>>? topSectionsData;
  int? curIdx;

  NoticeBoardVmState({
    List<Map<String, dynamic>>? topSectionsData,
    this.curIdx = 0,
  }) : topSectionsData = topSectionsData?? [
    {
      "title": S.current.announcement,
      "icon": Assets.noticeBoardAnnouncementIcon,
      "pageStartInstanceFn": AnnounPage.startInstance,
      "page": const AnnounPage(),
    },
    {
      "title": S.current.event,
      "icon": Assets.noticeBoardEventIcon,
      "pageStartInstanceFn": EventPage.startInstance,
      "page": const EventPage(),
    },
    {
      "title": S.current.documents,
      "icon": Assets.noticeBoardDocumentsIcon,
      "pageStartInstanceFn": DocumentsPage.startInstance,
      "page": const DocumentsPage(),
    },
  ];

  NoticeBoardVmState copyWith({
    List<Map<String, dynamic>>? topSectionsData,
    int? curIdx = 0,
  }) {
    return NoticeBoardVmState(
      topSectionsData: topSectionsData ?? this.topSectionsData,
      curIdx: curIdx ?? 0,
    );
  }
}

@riverpod
class NoticeBoardVm extends _$NoticeBoardVm {
  get topSectionsData => state.topSectionsData;

  NoticeBoardVmState initState() {
    return NoticeBoardVmState();
  }

  @override
  NoticeBoardVmState build(){
    final state = initState();
    Log.d("--------------------------build---------------------");

    // 初始时导航到子路由
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // switchPage(state.curIdx ?? 0, null, true);
    });

    return state;
  }

  // 页面切换
  switchPage(int index,BuildContext? context, [bool? isFirstInitSwitch] ){
    if(state.curIdx != index){
      state = state.copyWith(curIdx: index);
      final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
      // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
      final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
      pageStartInstanceFn(context:context);
    }else {
      if(isFirstInitSwitch??false){
        final List<Map<String, dynamic>>? topSectionsData = state.topSectionsData;
        // Log.d("当前页面${topSectionsData?[index]['pageStartInstanceFn']}");
        final pageStartInstanceFn = topSectionsData?[index]['pageStartInstanceFn'] as Function({BuildContext? context});
        pageStartInstanceFn(context:context);
      }
    }
  }

}


