import 'package:cpnav/controllers/appcontroller.dart';
import 'package:cpnav/controllers/gnss_Controller.dart';
import 'package:cpnav/pages/setting/child_pages/antenna/antenna_setting.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:scence_map/controllers/controller.dart';
import 'package:scence_map/controllers/plumController.dart';
import 'appbar.dart';
import 'pages/aim_point/aimpointController.dart';
import 'pages/history/history_record.dart';
import 'pages/pile/pileNav/view.dart';
import 'pages/pile/rightDra/pileGenerate.dart';
import 'pages/real/index.dart';
import 'pages/setting/child_pages/XyChange/connect.dart';
import 'pages/setting/setting_page.dart';
import 'service/user/loginprefs.dart';

final AppController appcontroller = Get.put(AppController());
Connect connect = Connect();
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); //设置全屏
  Get.put(ScenceMapController());
  Get.put(PlumDataController());
  Get.put(AimPointerController());
  Get.put(GnssController());
  Get.put(AntennaController());
  LoginPrefs loginPrefs = LoginPrefs();
  String value =
      await loginPrefs.init(); // await 关键字必须用在异步方法中 await等待异步方法执行完毕 异步方法必须用变量接收
  if ('ok' == value) {
    connect.init();
    runApp(const MyApp());
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: appcontroller.isDarkMode.value
            ? ThemeData.dark()
            : ThemeData.light(),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      );
    });
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late bool isPortrait;
  late double appBarHeight = 34.0;
  final _currentIndex = 0.obs;
  final List<Widget> _pages = [
    Real(),
    RealView(),
    Container(
      color: Colors.blue,
    ),
    HistoryRecord(),
    SettingPortrait()
  ];
  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;

    return Scaffold(
      resizeToAvoidBottomInset: false,
      drawerEdgeDragWidth: 0.0, // 禁止通过滑动打开drawer
      endDrawer: _currentIndex.value == 1
          ? Drawer(
              width: size.width * .8,
              child: const PileGenerate(),
            )
          : null,
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(appBarHeight),
        child: CustomAppBar(
          appBarHeight: 56,
          currentIndex: _currentIndex,
        ),
      ),
      body: OrientationBuilder(
        builder: (context, orientation) {
          // final size = MediaQuery.of(context).size;
          appBarHeight = Orientation.portrait == orientation ? 56.0 : 34.0;
          isPortrait = Orientation.portrait == orientation ? true : false;
          appBarHeight = Orientation.portrait == orientation ? 56.0 : 34.0;
          if (!isPortrait) {
            return Row(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Expanded(
                    child: _pages[_currentIndex.value],
                  ),
                  const VerticalDivider(
                    width: 1, // 设置高度为1
                    thickness: 1,
                  ),
                  SizedBox(
                    width: 48,
                    child: SafeArea(
                      child: LayoutBuilder(builder: (context, constraints) {
                        // 根据屏幕宽度判断横屏或竖屏
                        // if (!isPortrait) {
                        return Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: [
                              Column(
                                children: [
                                  IconButton(
                                    onPressed: () {
                                      setState(() {
                                        _currentIndex.value = 0;
                                      });
                                    },
                                    icon: const Icon(Icons.date_range_rounded),
                                    color: _currentIndex.value == 0
                                        ? const Color.fromARGB(255, 60, 95, 123)
                                        : Colors.grey,
                                  ),
                                  Align(
                                    alignment: Alignment.center,
                                    child: Text(
                                      '实时',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: _currentIndex.value == 0
                                            ? const Color.fromARGB(
                                                255, 60, 95, 123)
                                            : Colors.grey,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              const Divider(
                                height: 1, // 设置高度为1
                                thickness: 1,
                              ),
                              //
                              Column(
                                children: [
                                  IconButton(
                                    onPressed: () {
                                      setState(() {
                                        _currentIndex.value = 1;
                                      });
                                    },
                                    icon: const Icon(Icons.map_outlined),
                                    color: _currentIndex.value == 1
                                        ? const Color.fromARGB(255, 60, 95, 123)
                                        : Colors.grey,
                                  ),
                                  Align(
                                    alignment: Alignment.center,
                                    child: Text(
                                      '桩点',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: _currentIndex.value == 1
                                            ? const Color.fromARGB(
                                                255, 60, 95, 123)
                                            : Colors.grey,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              const Divider(),
                              Column(
                                children: [
                                  IconButton(
                                    onPressed: () {
                                      setState(() {
                                        _currentIndex.value = 2;
                                      });
                                    },
                                    icon: const Icon(Icons.my_location_sharp),
                                    color: _currentIndex.value == 2
                                        ? const Color.fromARGB(255, 60, 95, 123)
                                        : Colors.grey,
                                  ),
                                  Align(
                                    alignment: Alignment.center,
                                    child: Text(
                                      '任务',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: _currentIndex.value == 2
                                            ? const Color.fromARGB(
                                                255, 60, 95, 123)
                                            : Colors.grey,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              const Divider(),
                              Column(
                                children: [
                                  IconButton(
                                    onPressed: () {
                                      setState(() {
                                        _currentIndex.value = 3;
                                      });
                                    },
                                    icon:
                                        const Icon(Icons.table_chart_outlined),
                                    color: _currentIndex.value == 3
                                        ? const Color.fromARGB(255, 60, 95, 123)
                                        : Colors.grey,
                                  ),
                                  Align(
                                    alignment: Alignment.center,
                                    child: Text(
                                      '历史',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: _currentIndex.value == 3
                                            ? const Color.fromARGB(
                                                255, 60, 95, 123)
                                            : Colors.grey,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              const Divider(),
                              Column(
                                children: [
                                  IconButton(
                                    onPressed: () {
                                      setState(() {
                                        _currentIndex.value = 4;
                                      });
                                    },
                                    icon: const Icon(Icons.settings),
                                    color: _currentIndex.value == 4
                                        ? const Color.fromARGB(255, 60, 95, 123)
                                        : Colors.grey,
                                  ),
                                  Align(
                                    alignment: Alignment.center,
                                    child: Text(
                                      '设置',
                                      style: TextStyle(
                                        fontSize: 12,
                                        color: _currentIndex.value == 4
                                            ? const Color.fromARGB(
                                                255, 60, 95, 123)
                                            : Colors.grey,
                                      ),
                                    ),
                                  ),
                                ],
                              )
                            ]);
                        //
                      }),
                    ),
                  )
                ]);
          } //

          else {
            // 竖屏布局,保持原有底部导航栏样式
            return Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Expanded(
                    flex: 12,
                    child: _pages[_currentIndex.value],
                  ),
                  // VerticalDivider(),
                  Expanded(
                    flex: 1,
                    child: SizedBox(
                      height: 48,
                      child: BottomNavigationBar(
                          type: BottomNavigationBarType.fixed,
                          currentIndex: _currentIndex.value,
                          onTap: (index) {
                            setState(() {
                              _currentIndex.value = index;
                            });
                          },
                          items: const [
                            BottomNavigationBarItem(
                                icon: Icon(Icons.date_range_rounded),
                                label: "实时"),
                            BottomNavigationBarItem(
                                icon: Icon(Icons.map_outlined), label: "计划"),
                            BottomNavigationBarItem(
                                icon: Icon(Icons.my_location_sharp),
                                label: "对点"),
                            BottomNavigationBarItem(
                                icon: Icon(Icons.table_chart_outlined),
                                label: "历史"),
                            BottomNavigationBarItem(
                                icon: Icon(Icons.settings), label: "设置"),
                          ]),
                    ),
                  )
                ]);
          }
        },
      ),
    );
  }
}