import 'package:flutter/material.dart';
import '../controllers/add_subscription_controller.dart';
import '../widgets/add_subscription/add_subscription_app_bar.dart';
import '../widgets/add_subscription/add_subscription_header.dart';
import '../widgets/add_subscription/add_subscription_form.dart';
import '../widgets/add_subscription/add_subscription_event_section.dart';
import '../widgets/add_subscription/add_subscription_save_button.dart';
// import '../theme/app_colors.dart';

/// 새로운 구독을 추가하는 화면
class AddSubscriptionScreen extends StatefulWidget {
  const AddSubscriptionScreen({super.key});

  @override
  State<AddSubscriptionScreen> createState() => _AddSubscriptionScreenState();
}

class _AddSubscriptionScreenState extends State<AddSubscriptionScreen>
    with SingleTickerProviderStateMixin {
  late AddSubscriptionController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AddSubscriptionController(context: context);
    _controller.initialize(vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _onScroll() {
    setState(() {
      _controller.scrollOffset = _controller.scrollController.offset;
    });
  }

  @override
  Widget build(BuildContext context) {
    // 스크롤 리스너 추가
    _controller.scrollController.removeListener(_onScroll);
    _controller.scrollController.addListener(_onScroll);

    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.surface,
      extendBodyBehindAppBar: true,
      appBar: AddSubscriptionAppBar(
        controller: _controller,
        scrollOffset: _controller.scrollOffset,
        onScanSMS: () => _controller.scanSMS(setState: setState),
      ),
      body: SingleChildScrollView(
        controller: _controller.scrollController,
        physics: const BouncingScrollPhysics(),
        padding: const EdgeInsets.all(16),
        child: Form(
          key: _controller.formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: MediaQuery.of(context).padding.top + 60),

              // 헤더 섹션
              AddSubscriptionHeader(
                controller: _controller,
                fadeAnimation: _controller.fadeAnimation!,
                slideAnimation: _controller.slideAnimation!,
              ),

              // 서비스 정보 폼
              AddSubscriptionForm(
                controller: _controller,
                fadeAnimation: _controller.fadeAnimation!,
                slideAnimation: _controller.slideAnimation!,
                setState: setState,
              ),
              const SizedBox(height: 16),

              // 이벤트/할인 섹션
              AddSubscriptionEventSection(
                controller: _controller,
                fadeAnimation: _controller.fadeAnimation!,
                slideAnimation: _controller.slideAnimation!,
                setState: setState,
              ),
              const SizedBox(height: 32),

              // 저장 버튼
              AddSubscriptionSaveButton(
                controller: _controller,
                fadeAnimation: _controller.fadeAnimation!,
                slideAnimation: _controller.slideAnimation!,
                setState: setState,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
