import 'dart:convert';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:mohem_flutter_app/api/pending_transactions_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.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/get_announcement_details.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:url_launcher/url_launcher.dart';

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

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

class _AnnouncementDetailsState extends State<AnnouncementDetails> {
  String jsonResponse = "";
  int currentPageNo = 0;
  int rowID = 0;

  GetAnnouncementDetails? getAnnouncementDetailsObj;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    getRequestID();
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(
        context,
        title: LocaleKeys.announcements.tr(),
      ),
      body: getAnnouncementDetailsObj == null
          ? const SizedBox()
          : SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  SizedBox(
                    width: double.infinity,
                    height: 150.0,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(10),
                      child: Image.memory(
                        base64Decode(Utils.getBase64FromJpeg(getAnnouncementDetailsObj?.bannerImage)),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Html(
                    data: getAnnouncementDetailsObj?.bodyEN,
                  ).paddingOnly(top: 12),
                ],
              ).objectContainerView().paddingAll(21),
            ),
    );
  }

  void getRequestID() {
    if (currentPageNo == 0) {
      Map arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;
      currentPageNo = arguments["currentPageNo"];
      rowID = arguments["rowID"];
      getAnnouncementDetails(0, rowID);
    }
  }

  Future getAnnouncementDetails(int itgAwarenessID, int itgRowID) async {
    try {
      Utils.showLoading(context);
      getAnnouncementDetailsObj = await PendingTransactionsApiClient().getAnnouncementDetails(itgAwarenessID, currentPageNo, itgRowID);
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }
}
