import 'package:chepuhagram/presentation/screens/login_screen.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '/logic/auth_provider.dart';
import '/core/theme_manager.dart';

class SettingsScreen extends StatelessWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final themeProv = context.watch<ThemeProvider>();
    final authProv = context.read<AuthProvider>();

    return Scaffold(
      appBar: AppBar(title: const Text("Настройки")),
      body: Column(
        children: [
          // Секция Профиля
          const UserAccountsDrawerHeader(
            accountName: Text("Artur Karasevich"),
            accountEmail: Text("@ArturKarasevich"),
            currentAccountPicture: CircleAvatar(
              child: Icon(Icons.person, size: 40),
            ),
            decoration: BoxDecoration(color: Colors.transparent),
          ),

          const Divider(),

          SwitchListTile(
            secondary: const Icon(Icons.dark_mode),
            title: const Text("Ночной режим"),
            value: themeProv.themeMode == ThemeMode.dark,
            onChanged: (val) => themeProv.toggleTheme(val),
          ),

          // Выбор цвета акцента
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Icon(Icons.palette_outlined),
                    SizedBox(width: 10),
                    const Text("Цвет темы"),
                    Spacer(),
                    _colorCircle(context, const Color(0xFF24A1DE), themeProv),
                    _colorCircle(context, const Color(0xFF3E8E7E), themeProv),
                    _colorCircle(context, const Color(0xFF8E3E7E), themeProv),
                    _colorCircle(context, const Color(0xFFFF9800), themeProv),
                    _colorCircle(context, const Color(0xFFF44336), themeProv),
                  ],
                ),
              ],
            ),
          ),

          const Divider(),

          // Выход
          ListTile(
            leading: const Icon(Icons.exit_to_app, color: Colors.red),
            title: const Text(
              "Выйти из аккаунта",
              style: TextStyle(color: Colors.red),
            ),
            onTap: () async {
              await authProv.logout();
              if (context.mounted) {
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(builder: (_) => const LoginScreen()),
                );
              }
            },
          ),
          const Spacer(),
          const Center(
            child: Text(
              "Chepuhagram for Android v1.0.0",
              style: TextStyle(color: Colors.grey, fontSize: 12),
            ),
          ),
          const Center(
            child: Text(
              "Made by ArturKarasevich",
              style: TextStyle(color: Colors.grey, fontSize: 12),
            ),
          ),
          SizedBox(height: 10,)
        ],
      ),
    );
  }

  Widget _colorCircle(BuildContext context, Color color, ThemeProvider prov) {
    bool isSelected = prov.accentColor == color;
    return GestureDetector(
      onTap: () => prov.updateAccentColor(color),
      child: Container(
        padding: const EdgeInsets.all(2),
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(
            color: isSelected ? color : Colors.transparent,
            width: 2,
          ),
        ),
        child: CircleAvatar(backgroundColor: color, radius: 15),
      ),
    );
  }
}
