import 'package:flutter/material.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:go_router/go_router.dart';

import 'package:fotodocumentation/pages/ui_utils/general_style.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/global_router.dart';

class PageHeaderWidget extends StatelessWidget {
  LoginCredentials get loginCredentials => DiContainer.get();

  final String text;
  final String subText;

  const PageHeaderWidget({super.key, required this.text, this.subText = ""});

  GeneralStyle get _generalStyle => DiContainer.get();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 24.0, bottom: 24.0),
      child: LayoutBuilder(
        builder: (context, constraints) {
          final isNarrow = constraints.maxWidth < 600;
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (isNarrow) ...[
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    _logoutButton(context),
                    Image.asset(
                      'assets/images/logo.png',
                      height: 48,
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Text(
                  key: Key("PageHeaderTextHeadline"),
                  text,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 50,
                    fontFamily: _generalStyle.fontFamily,
                    color: _generalStyle.primaryTextLabelColor,
                  ),
                ),
              ] else ...[
                Row(
                  children: [
                    Expanded(
                      child: Text(
                        key: Key("PageHeaderTextHeadline"),
                        text,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 50,
                          fontFamily: _generalStyle.fontFamily,
                          color: _generalStyle.primaryTextLabelColor,
                        ),
                      ),
                    ),
                    _logoutButton(context),
                    Image.asset(
                      'assets/images/logo.png',
                      height: 48,
                    ),
                  ],
                ),
              ],
              if (subText.isNotEmpty) ...[
                const SizedBox(height: 16),
                Text(
                  key: Key("PageHeaderTextSubHeadline"),
                  subText,
                  style: TextStyle(
                    fontSize: 16,
                    fontFamily: _generalStyle.fontFamily,
                    color: _generalStyle.secondaryTextLabelColor,
                  ),
                ),
              ],
            ],
          );
        },
      ),
    );
  }

  Widget _logoutButton(BuildContext context) {
    return ElevatedButton.icon(
      onPressed: () {
        loginCredentials.logout();
        context.go(GlobalRouter.pathLogin);
      },
      icon: Icon(
        Icons.logout_rounded,
        color: _generalStyle.secondaryTextLabelColor,
        size: 24,
      ),
      label: Text(
        "logout",
        style: TextStyle(
          fontFamily: _generalStyle.fontFamily,
          fontWeight: FontWeight.normal,
          fontSize: 16,
          color: _generalStyle.secondaryTextLabelColor,
        ),
      ),
      style: ElevatedButton.styleFrom(
        backgroundColor: Colors.white,
        elevation: 0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
    );
  }
}
