import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/components/alert_dialog.dart';
import 'package:vitalapp/components/side_nav/defines.dart';
import 'package:vitalapp/components/side_nav/side_nav.dart';
import 'package:vitalapp/pages/settings/about/view.dart';
import 'package:vitalapp/pages/settings/devices/controller.dart';
import 'package:vitalapp/pages/settings/devices/view.dart';
import 'package:vitalapp/pages/settings/server/controller.dart';
import 'package:vitalapp/pages/settings/server/view.dart';
import 'package:vitalapp/routes/nav_ids.dart';
import 'package:vitalapp/routes/route_setting.dart';

import 'controller.dart';

class SettingCenterPage extends GetView<SettingCenterController> {
  const SettingCenterPage({super.key});

  @override
  Widget build(BuildContext context) {
    return VSideNavView(
      navId: NavIds.SETTINGS,
      items: _buildItems(),
    );
  }

  List<VSideNavMenuItem> _buildItems() {
    final items = <VSideNavMenuItem>[];
    items.add(_buildDataSyncItem());
    items.add(_buildDevicesSettingItem());
    items.add(_buildServerSettingItem());
    items.add(_buildAboutItem());
    items.add(_buildLogOutItem());
    return items;
  }

  VSideNavMenuItem _buildDataSyncItem() {
    return VSideNavMenuItem(
      title: "数据同步",
      icon: Icon(Icons.sync, color: Colors.grey.shade700),
      onTap: () {
        Get.toNamed("/data_sync");
      },
    );
  }

  VSideNavMenuItem _buildDevicesSettingItem() {
    return VSideNavMenuItem(
      title: "设备管理",
      icon: Icon(Icons.gamepad_outlined, color: Colors.grey.shade700),
      route: VRouteSetting(
        "/devices",
        () => const DevicesSettingPage(),
        binding: BindingsBuilder(
          () {
            Get.lazyPut(() => DevicesSettingController());
          },
        ),
      ),
    );
  }

  VSideNavMenuItem _buildServerSettingItem() {
    return VSideNavMenuItem(
      title: "服务设置",
      icon: Icon(Icons.electrical_services, color: Colors.grey.shade700),
      route: VRouteSetting(
        "/server",
        () => const ServerSettingPage(),
        binding: BindingsBuilder(
          () {
            Get.lazyPut(() => ServerSettingController());
          },
        ),
      ),
    );
  }

  VSideNavMenuItem _buildAboutItem() {
    return VSideNavMenuItem(
      title: "关于我们",
      icon: Icon(Icons.info_outline, color: Colors.grey.shade700),
      onTap: () {
        // TODO: 用confirm组件替换
        Get.dialog(
          VAlertDialog(
            // title: "提示",
            width: 400,
            content: Container(
              height: 160,
              padding: const EdgeInsets.symmetric(horizontal: 24),
              alignment: Alignment.center,
              child: const AboutPage(),
            ),
            showCancel: false,
            onConfirm: () {
              // controller.logOut();
              Get.back();
            },
          ),
          barrierDismissible: false,
          barrierColor: Colors.black.withOpacity(.4),
        );
      },
      // route: VRouteSetting("/about", () => const AboutPage()),
    );
  }

  VSideNavMenuItem _buildLogOutItem() {
    return VSideNavMenuItem(
      title: "退出登录",
      icon: Icon(Icons.exit_to_app, color: Colors.grey.shade700),
      // shouldRearrage: true, // TODO: 调整样式后启用
      onTap: () {
        // TODO: 用confirm组件替换
        Get.dialog(
          VAlertDialog(
            title: "提示",
            width: 260,
            content: Container(
              height: 32,
              padding: const EdgeInsets.symmetric(horizontal: 24),
              alignment: Alignment.center,
              child: const Text(
                "确定退出登录？",
                style: TextStyle(fontSize: 20),
              ),
            ),
            onConfirm: () {
              controller.logOut();
            },
          ),
          barrierDismissible: false,
          barrierColor: Colors.black.withOpacity(.4),
        );
      },
    );
  }
}
