import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/extensions/int_extensions.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.dart';

enum CommonHeaderEnum { ZoomIn, ZoomOut, ShareAsText, ShareAsImage }

class CommonHeader extends StatelessWidget {
  final String title;
  final Color gradiantColor;
  final String assetImage;
  final double ratio;
  final bool isCustom;
  final String message;

  final Function(CommonHeaderEnum) onTap;

  CommonHeader(this.title, this.assetImage, this.gradiantColor, {Key key, this.ratio, this.isCustom = false, this.message, this.onTap}) : assert((isCustom == true && message == null) ? false : true);

  @override
  Widget build(BuildContext context) {
    bool isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;

    return AspectRatio(
      aspectRatio: ratio ?? 469 / 313,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Container(
            decoration: BoxDecoration(
              color: Colors.transparent,
              image: DecorationImage(
                fit: BoxFit.cover,
                image: AssetImage(assetImage),
              ),
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              gradient: LinearGradient(
                begin: FractionalOffset.bottomLeft,
                end: FractionalOffset.topRight,
                colors: [
                  Color(0x00FFFFFF),
                  gradiantColor.withOpacity(.75),
                  gradiantColor,
                ],
                stops: [0.0, 0.45, 1.0],
              ),
            ),
          ),
          isCustom
              ? Column(
                  mainAxisAlignment: isPortrait ? MainAxisAlignment.end : MainAxisAlignment.center,
                  children: [
                    title.toText(30),
                    message.toText(16, textAlign: TextAlign.center, height: 20).paddingOnly(top: 16, left: 50, right: 50),
                    8.height,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        PopupMenuButton(
                          child: SvgPicture.asset("assets/icons/new/share.svg", height: 20, width: 20, color: Colors.white),
                          padding: EdgeInsets.fromLTRB(4, 4, 0, 4),
                          itemBuilder: (_) => <PopupMenuItem<int>>[
                            PopupMenuItem(
                              value: 1,
                              child: Text(
                                "مشاركة رابط",
                                style: TextStyle(color: ColorConsts.primaryBlack),
                              ),
                            ),
                            PopupMenuItem(
                              value: 2,
                              child: Text(
                                "مشاركة صورة",
                                style: TextStyle(color: ColorConsts.primaryBlack),
                              ),
                            )
                          ],
                          onSelected: (int value) {
                            if (value == 1) {
                              onTap(CommonHeaderEnum.ShareAsText);
                            } else {
                              onTap(CommonHeaderEnum.ShareAsImage);
                            }
                          },
                        ),
                        12.width,
                        SvgPicture.asset("assets/icons/new/zoom_out.svg", height: 20, width: 20, color: Colors.white).onPress(() {
                          onTap(CommonHeaderEnum.ZoomOut);
                        }),
                        12.width,
                        SvgPicture.asset("assets/icons/new/zoom_in.svg", height: 20, width: 20, color: Colors.white).onPress(() {
                          onTap(CommonHeaderEnum.ZoomIn);
                        }),
                      ],
                    ),
                    if (isPortrait) 32.height,
                  ],
                )
              : Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Image.asset('assets/icons/new/Tangeem-logo-W.png', width: 50),
                    26.height,
                    title.toText(30),
                  ],
                ).paddingOnly(right: isPortrait ? 0 : 120),
        ],
      ),
    );
  }
}
