import 'package:flutter/material.dart';
import 'changepassword.dart';
import 'common/settings.dart';


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

  @override
  _CreatePasswordScreenState createState() => _CreatePasswordScreenState();
}

class _CreatePasswordScreenState extends State<CreatePasswordScreen> {
  final _formKey = GlobalKey<FormState>();
  final _newPasswordController = TextEditingController();
  final _confirmPasswordController = TextEditingController();

  bool _obscureNew = true;
  bool _obscureConfirm = true;

  @override
  void dispose() {
    _newPasswordController.dispose();
    _confirmPasswordController.dispose();
    super.dispose();
  }

  Future<void> _savePassword() async {
    if (!_formKey.currentState!.validate()) return;

    // TODO: call your API to set password here
    // await yourApi.setPassword(_newPasswordController.text);

    // Optional: show success then navigate
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text("Password changed successfully")),
    );

    // Small delay so the SnackBar is noticeable (optional)
    await Future.delayed(const Duration(milliseconds: 300));

    if (!mounted) return;

    // ✅ Navigate to Select Tour Tanker page and clear back stack
    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(
        builder: (_) =>  ChangePasswordScreen(), // replace with your actual page
      ),
          (route) => false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true, // shifts UI up when keyboard opens
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        surfaceTintColor: Colors.transparent, // <- remove M3 tint
        scrolledUnderElevation: 0,

        title: Text(
          "Change Password",
          style: fontTextStyle(14,  Color(0xFF101214), FontWeight.w500),
        ),
        iconTheme: IconThemeData(color: Color(0XFF2A2A2A)),
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Padding(
            padding:
            const EdgeInsets.fromLTRB(8, 8, 8, 8), // Add padding if needed
            child: Image.asset(
              'images/backbutton_appbar.png', // Replace with your image path
              fit: BoxFit.contain,
              color: Color(0XFF2A2A2A),
              height: 24,
              width: 24,
            ),
          ),
        ),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20.0),
        physics: const BouncingScrollPhysics(),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // Centered title & subtitle with top padding
              Padding(
                padding: const EdgeInsets.only(top: 40, bottom: 24),
                child: Column(
                  children: [
                    Text(
                      "Create Password",
                      textAlign: TextAlign.center,
                      style: fontTextStyle(16, Color(0xFF000000),FontWeight.w800,),
                    ),
                    SizedBox(height: 6),
                    Text(
                      "Create a strong password for your account",
                      textAlign: TextAlign.center,
                      style: fontTextStyle(12,  Color(0xFF7E7F80), FontWeight.w400,
                      ),
                    ),
                  ],
                ),
              ),

              // New Password
              Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  "New Password*",
                  style: fontTextStyle(12, Color(0xFF101214), FontWeight.w700,
                  ),
                ),
              ),
              const SizedBox(height: 8),
              TextFormField(
                controller: _newPasswordController,
                obscureText: _obscureNew,
                decoration: InputDecoration(
                  border: const OutlineInputBorder(),
                  hintText: "Enter new password",
                  hintStyle: fontTextStyle(14,  Color(0xFF2A2A2A), FontWeight.w400,
                  ),
                  suffixIcon: IconButton(
                    icon: Icon(
                      _obscureNew ? Icons.visibility_off : Icons.visibility,
                    ),
                    onPressed: () => setState(() {
                      _obscureNew = !_obscureNew;
                    }),
                  ),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return "Please enter new password";
                  }
                  if (value.length < 6) {
                    return "Password must be at least 6 characters";
                  }
                  return null;
                },
              ),
              const SizedBox(height: 20),

              // Confirm Password
              Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  "Confirm Password*",
                  style: fontTextStyle(12,  Color(0xFF101214), FontWeight.w700,
                  ),
                ),
              ),
              const SizedBox(height: 8),
              TextFormField(
                controller: _confirmPasswordController,
                obscureText: _obscureConfirm,
                decoration: InputDecoration(
                  border: const OutlineInputBorder(),
                  hintText: "Confirm new password",
                  hintStyle: fontTextStyle(14,  Color(0xFF2A2A2A), FontWeight.w400,
                  ),
                  suffixIcon: IconButton(
                    icon: Icon(
                      _obscureConfirm ? Icons.visibility_off : Icons.visibility,
                    ),
                    onPressed: () => setState(() {
                      _obscureConfirm = !_obscureConfirm;
                    }),
                  ),
                ),
                validator: (value) {
                  if (value != _newPasswordController.text) {
                    return "Passwords do not match";
                  }
                  return null;
                },
              ),
              const SizedBox(height: 30),

              // Save & Login Button
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: _savePassword,
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    backgroundColor: Color(0XFF101214),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(30),
                    ),
                  ),
                  child: Text(
                    "Save and Continue",
                    style: fontTextStyle(14,  Color(0xFFFFFFFF), FontWeight.w600,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Select Tour Tanker")),
      body: const Center(
        child: Text(
          "Select Tour Tanker page goes here",
          style: TextStyle(fontSize: 16),
        ),
      ),
    );
  }
}
