import 'dart:convert';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/date_uitl.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/items_for_sale/get_items_for_sale_list.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/circular_avatar.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';
import 'package:url_launcher/url_launcher.dart';

class ItemForSaleDetailPage extends StatefulWidget {
  const ItemForSaleDetailPage({Key? key}) : super(key: key);

  @override
  State<ItemForSaleDetailPage> createState() => _ItemForSaleDetailPageState();
}

class _ItemForSaleDetailPageState extends State<ItemForSaleDetailPage> {
  late GetItemsForSaleList getItemsForSaleList;

  @override
  Widget build(BuildContext context) {
    getItemsForSaleList = ModalRoute.of(context)?.settings.arguments as GetItemsForSaleList;

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(context, title: LocaleKeys.itemsForSale.tr(), showHomeButton: true),
      body: Column(
        children: [
          ListView(
            padding: const EdgeInsets.all(21),
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Hero(
                    tag: "ItemImage" + getItemsForSaleList.itemSaleID.toString(),
                    transitionOnUserGestures: true,
                    child: AspectRatio(
                      aspectRatio: 322 / 261,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(6),
                        child: Image.memory(
                          base64Decode(getItemsForSaleList.itemAttachments![0].content!),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  ).paddingAll(8),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      getItemsForSaleList.title!.toText20(),
                      getItemsForSaleList.description!.toText12(maxLine: 5, color: MyColors.grey57Color),
                      21.height,
                      getItemsForSaleList.status!.toText16(color: getItemsForSaleList.status == 'Approved' ? MyColors.greenColor : MyColors.yellowColor),
                      "${getItemsForSaleList.quotePrice} ${getItemsForSaleList.currencyCode!}".toText20(isBold: true),
                      1.divider.paddingOnly(top: 16, bottom: 16),
                      Row(
                        children: [
                          CircularAvatar(
                            url: getItemsForSaleList.profilePicture,
                            height: 34,
                            width: 34,
                          ),
                          7.width,
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: [getItemsForSaleList.fullName!.toText14(), ItemDetailView("Posted on", DateUtil.formatDateToDate(DateTime.parse(getItemsForSaleList.created!), false))],
                          ).expanded,
                        ],
                      ),
                    ],
                  ).paddingOnly(right: 16, left: 16, top: 6, bottom: 16)
                ],
              ).objectContainerView(disablePadding: true),
            ],
          ).expanded,
          Row(
            children: [
              DefaultButton("Email", () async {
                Uri emailLaunchUri = Uri(
                  scheme: 'mailto',
                  path: getItemsForSaleList.emailAddress,
                );
                launchUrl(emailLaunchUri);
              }, iconData: Icons.email_sharp, isTextExpanded: false)
                  .expanded,
              8.width,
              DefaultButton("Call", () async {
                Uri callLaunchUri = Uri(
                  scheme: 'tel',
                  path: getItemsForSaleList.mobileNumber,
                );
                launchUrl(callLaunchUri);
              }, iconData: Icons.call_sharp, isTextExpanded: false)
                  .expanded,
            ],
          ).insideContainer,
        ],
      ),
    );
  }
}
