import 'dart:convert';

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/classes/utils.dart';
import 'package:mohem_flutter_app/models/get_announcement_details.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.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: "Announcements",
      ),
      body: SingleChildScrollView(
        child: Container(
          width: double.infinity,
          padding: const EdgeInsets.all(10.0),
          margin: const EdgeInsets.all(12.0),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10),
            boxShadow: [
              BoxShadow(
                color: const Color(0xff000000).withOpacity(.05),
                blurRadius: 26,
                offset: const Offset(0, -3),
              ),
            ],
          ),
          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,
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 12.0),
                child: Html(
                  data: getAnnouncementDetailsObj?.bodyEN,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

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

  void getAnnouncementDetails(int itgAwarenessID, int itgRowID) async {
    try {
      Utils.showLoading(context);
      jsonResponse = await PendingTransactionsApiClient().getAnnouncements(itgAwarenessID, currentPageNo, itgRowID);
      // todo '@haroon' move below post processing code to above method and get exact model which you need,

      var jsonDecodedData = jsonDecode(jsonDecode(jsonResponse)['result']['data']);
      getAnnouncementDetailsObj = GetAnnouncementDetails.fromJson(jsonDecodedData[0]);
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }
}
