import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'about/settings_about_index_view.dart';
import 'cubit/settings_index_cubit.dart';
import 'cubit/settings_index_state.dart';
import 'customer_service/customer_service_index_view.dart';
import 'factory_settings/factory_settings_index_view.dart';
import 'remote_control/remote_control_index_view.dart';
import 'software_update/software_update_index_view.dart';
import 'widgets/settings_side_menu.dart';

@RoutePage()
class SettingsIndexView extends StatelessWidget {
  const SettingsIndexView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => SettingsIndexCubit(),
      child: const _SettingsIndexContent(),
    );
  }
}

class _SettingsIndexContent extends StatelessWidget {
  const _SettingsIndexContent();

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<SettingsIndexCubit, SettingsIndexState>(
      builder: (context, state) {
        return Padding(
          padding: EdgeInsets.fromLTRB(30.w, 28.h, 30.w, 28.h),
          child: Row(
            children: [
              Expanded(
                flex: 25,
                child: SettingsSideMenu(
                  selectedMenu: state.selectedMenu,
                  onSelected: context.read<SettingsIndexCubit>().selectMenu,
                ),
              ),
              SizedBox(width: 24.w),
              Expanded(
                flex: 75,
                child: _buildSelectedPanel(state.selectedMenu),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildSelectedPanel(String selectedMenu) {
    switch (selectedMenu) {
      case '远程控制':
        return const RemoteControlIndexView();
      case '软件更新':
        return const SoftwareUpdateIndexView();
      case '客户服务':
        return const CustomerServiceIndexView();
      case '出厂设置':
        return const FactorySettingsIndexView();
      case '关于本机':
      default:
        return const SettingsAboutIndexView();
    }
  }
}
