import 'package:flutter/material.dart';

final Color _mainBlueColor = const Color(0xFF002FA7);
final Color _mainTextColor = const Color(0xFF1C1C1E);
final Color _subTextColor = const Color(0xFF6A717B);
final Color _pageBackgroundColor = const Color(0xFFF5F7F9);
final Color _cardBackgroundColor = Colors.white;
final Color _accentContainerColor = const Color(0xFFF0F2F5);
final Color _warningColor = Colors.redAccent;

const BoxShadow _cleanShadow = BoxShadow(
  color: Color.fromRGBO(0, 0, 0, 0.07),
  blurRadius: 8,
  offset: Offset(0, 4),
  spreadRadius: 0,
);


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

  @override
  State<SettingsScreen> createState() => _SettingsScreenState();
}

class _SettingsScreenState extends State<SettingsScreen> {

  bool _isPushEnabled = true;
  bool _isRentalAlertEnabled = true;
  bool _isStorageStatusAlert = true;
  bool _isAutoSyncEnabled = false;

  final String _currentAppVersion = "v1.0.0";


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: _pageBackgroundColor,
      appBar: AppBar(
        scrolledUnderElevation: 0,
        title: Text(
          '설정',
          style: TextStyle(
            fontWeight: FontWeight.w700,
            fontSize: 16,
            color: _mainTextColor,
          ),
        ),
        backgroundColor: _pageBackgroundColor,
        elevation: 0,
        centerTitle: false,
        actions: [
          IconButton(
            icon: Icon(Icons.more_vert, color: _mainTextColor),
            onPressed: () {},
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildSectionTitle('계정 및 보안'),
            const SizedBox(height: 12),
            _buildAccountSecurityCard(),

            const SizedBox(height: 24),
            _buildSectionTitle('알림 설정'),
            const SizedBox(height: 12),
            _buildNotificationCard(),

            const SizedBox(height: 24),
            _buildSectionTitle('앱 정보'),
            const SizedBox(height: 12),
            _buildAppInfoCard(),

            const SizedBox(height: 40),
            _buildWithdrawalButton(),
          ],
        ),
      ),
    );
  }

  Widget _buildSectionTitle(String title) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8.0),
      child: Text(
        title,
        style: TextStyle(
          color: _mainTextColor,
          fontSize: 15,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _buildCardWrapper({required Widget child}) {
    return Container(
      decoration: BoxDecoration(
        color: _cardBackgroundColor,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [_cleanShadow],
      ),
      child: child,
    );
  }

  Widget _buildNotificationCard() {
    return _buildCardWrapper(
      child: Column(
        children: [
          _buildToggleItem(
            '알림 전체 수신',
            '모든 알림을 켜고 끕니다.',
            _isPushEnabled,
                (val) => setState(() => _isPushEnabled = val),
            showDivider: true,
          ),
          _buildToggleItem(
            '대여/반납 알림',
            '대여 시작/종료, 반납 실패 알림',
            _isRentalAlertEnabled,
                (val) => setState(() => _isRentalAlertEnabled = val),
            showDivider: true,
          ),
          _buildToggleItem(
            '보관함 상태 알림',
            '살균 완료, 시스템 오류 등 상태 알림',
            _isStorageStatusAlert,
                (val) => setState(() => _isStorageStatusAlert = val),
            showDivider: true,
          ),
          _buildInfoLink('알림 시간대 설정', Icons.schedule_outlined),
          _buildDivider(16),
          _buildInfoLink('글자 크기 조정', Icons.format_size),
        ],
      ),
    );
  }

  Widget _buildAccountSecurityCard() {
    return _buildCardWrapper(
      child: Column(
        children: [
          _buildInfoLink('내 정보 수정', Icons.person_outline),
          _buildDivider(16),
          _buildInfoLink('비밀번호 변경', Icons.lock_outline),
          _buildDivider(16),
          _buildInfoLink('캐시 데이터 삭제', Icons.cleaning_services_outlined),
        ],
      ),
    );
  }

  Widget _buildAppInfoCard() {
    return _buildCardWrapper(
      child: Column(
        children: [
          _buildInfoLink('버전 정보', null, value: _currentAppVersion),
          _buildDivider(16),
          _buildInfoLink('이용 약관 및 정책', Icons.gavel),
          _buildDivider(16),
          _buildInfoLink('고객센터 및 FAQ', Icons.headset_mic_outlined),
          _buildDivider(16),
          _buildInfoLink('위치 권한 설정', Icons.location_on_outlined),
          _buildDivider(16),
          _buildInfoLink('서비스 지역 지도 보기', Icons.map_outlined),
        ],
      ),
    );
  }

  Widget _buildToggleItem(String title, String subtitle, bool value, ValueChanged<bool> onChanged, {required bool showDivider}) {
    return Column(
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        color: _mainTextColor,
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      subtitle,
                      style: TextStyle(
                        color: _subTextColor,
                        fontSize: 12,
                      ),
                    ),
                  ],
                ),
              ),
              Switch(
                value: value,
                onChanged: onChanged,
                activeColor: _mainBlueColor,
                inactiveThumbColor: _accentContainerColor,
                inactiveTrackColor: Colors.grey.shade400,
              ),
            ],
          ),
        ),
        if (showDivider)
          _buildDivider(0),
      ],
    );
  }

  Widget _buildInfoLink(String title, IconData? icon, {String? value}) {
    return InkWell(
      onTap: () { /* Navigation logic */ },
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
        child: Row(
          children: [
            if (icon != null) ...[
              Icon(icon, color: _mainTextColor, size: 20),
              const SizedBox(width: 12),
            ],
            Expanded(
              child: Text(
                title,
                style: TextStyle(color: _mainTextColor, fontSize: 14),
              ),
            ),
            if (value != null)
              Text(
                value,
                style: TextStyle(color: _subTextColor, fontSize: 14),
              )
            else
              Icon(Icons.arrow_forward_ios, color: _subTextColor, size: 16),
          ],
        ),
      ),
    );
  }

  Widget _buildDivider(double indent) {
    return Divider(color: _subTextColor.withOpacity(0.4), height: 1, thickness: 0.5, indent: indent, endIndent: 16);
  }

  Widget _buildWithdrawalButton() {
    return Center(
      child: TextButton(
        onPressed: () {},
        child: Text(
          '회원 탈퇴 신청',
          style: TextStyle(color: _warningColor, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }
}