import 'package:flutter/material.dart'; import 'package:manager_app/Components/multi_string_input_and_resource_container.dart'; import 'package:manager_app/Components/number_input_container.dart'; import 'package:manager_app/Components/resource_input_container.dart'; import 'package:manager_api_new/api.dart'; import 'package:manager_app/Screens/Configurations/Section/SubSection/Parcours/parcours_config.dart'; import 'package:manager_app/constants.dart'; class GameConfig extends StatefulWidget { final String? color; final String? label; final GameDTO initialValue; final ValueChanged onChanged; const GameConfig({ Key? key, this.color, this.label, required this.initialValue, required this.onChanged, }) : super(key: key); @override _GameConfigState createState() => _GameConfigState(); } class _GameConfigState extends State { late GameDTO gameDTO; @override void initState() { gameDTO = widget.initialValue; gameDTO.rows = gameDTO.rows ?? 3; gameDTO.cols = gameDTO.cols ?? 3; gameDTO.gameType = gameDTO.gameType ?? GameTypes.Puzzle; gameDTO.messageDebut = gameDTO.messageDebut ?? []; gameDTO.messageFin = gameDTO.messageFin ?? []; gameDTO.guidedPaths = gameDTO.guidedPaths ?? []; super.initState(); } @override void didUpdateWidget(GameConfig oldWidget) { super.didUpdateWidget(oldWidget); if (widget.initialValue.id != oldWidget.initialValue.id) { setState(() { gameDTO = widget.initialValue; gameDTO.rows = gameDTO.rows ?? 3; gameDTO.cols = gameDTO.cols ?? 3; gameDTO.gameType = gameDTO.gameType ?? GameTypes.Puzzle; }); } } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { int initialIndex = gameDTO.gameType?.value ?? 0; return DefaultTabController( key: ValueKey("${gameDTO.id}_$initialIndex"), length: 3, initialIndex: initialIndex, child: Builder(builder: (context) { final TabController controller = DefaultTabController.of(context); // Attach listener to sync gameType when tab changes controller.addListener(() { if (!controller.indexIsChanging) { GameTypes newType = GameTypes.values[controller.index]; if (gameDTO.gameType != newType) { setState(() { gameDTO.gameType = newType; }); widget.onChanged(gameDTO); } } }); return Column( children: [ TabBar( labelColor: kPrimaryColor, unselectedLabelColor: Colors.grey, indicatorColor: kPrimaryColor, tabs: [ Tab(icon: Icon(Icons.extension), text: "Puzzle"), Tab(icon: Icon(Icons.grid_on), text: "Puzzle Glissant"), Tab(icon: Icon(Icons.door_front_door), text: "Escape Game"), ], ), Container( height: 650, child: TabBarView( children: [ _buildPuzzleConfig(), _buildPuzzleConfig(), ParcoursConfig( initialValue: gameDTO.guidedPaths ?? [], parentId: gameDTO.id!, isEvent: false, isEscapeMode: true, onChanged: (paths) { setState(() { gameDTO.guidedPaths = paths; widget.onChanged(gameDTO); }); }, ), ], ), ), ], ); }), ); } Widget _buildPuzzleConfig() { return SingleChildScrollView( child: Column( children: [ Padding( padding: const EdgeInsets.only(top: 16.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ ResourceInputContainer( label: "Image du puzzle :", initialValue: gameDTO.puzzleImageId ?? '', color: kPrimaryColor, onChanged: (ResourceDTO resourceDTO) { setState(() { if (resourceDTO.id == null) { gameDTO.puzzleImageId = null; gameDTO.puzzleImage = null; } else { gameDTO.puzzleImageId = resourceDTO.id; gameDTO.puzzleImage = resourceDTO; } widget.onChanged(gameDTO); }); }, ), Flexible( child: MultiStringInputAndResourceContainer( label: "Message départ :", modalLabel: "Message départ", fontSize: 20, color: kPrimaryColor, initialValue: gameDTO.messageDebut ?? [], resourceTypes: [ ResourceType.Image, ResourceType.ImageUrl, ResourceType.VideoUrl, ResourceType.Video, ResourceType.Audio ], onGetResult: (value) { setState(() { gameDTO.messageDebut = value; widget.onChanged(gameDTO); }); }, maxLines: 1, isTitle: false, ), ), Flexible( child: MultiStringInputAndResourceContainer( label: "Message fin :", modalLabel: "Message fin", fontSize: 20, color: kPrimaryColor, initialValue: gameDTO.messageFin ?? [], resourceTypes: [ ResourceType.Image, ResourceType.ImageUrl, ResourceType.VideoUrl, ResourceType.Video, ResourceType.Audio ], onGetResult: (value) { setState(() { gameDTO.messageFin = value; widget.onChanged(gameDTO); }); }, maxLines: 1, isTitle: false, ), ), ], ), ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ NumberInputContainer( label: "Lignes :", initialValue: gameDTO.rows ?? 3, isSmall: true, onChanged: (String value) { setState(() { gameDTO.rows = int.tryParse(value) ?? 3; widget.onChanged(gameDTO); }); }, ), NumberInputContainer( label: "Colonnes :", initialValue: gameDTO.cols ?? 3, isSmall: true, onChanged: (String value) { setState(() { gameDTO.cols = int.tryParse(value) ?? 3; widget.onChanged(gameDTO); }); }, ), ], ), ], ), ); } }