import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../models/accountStatementResponseModel.dart';
import '../../../../res/app_colors.dart';
import '../../controllers/account_statement_controller.dart';

class AccountStatementWidget extends StatefulWidget {
  // bool isCredit = false;
  // String transactionAmount = "";
  // String transactionDate = "";
  // String accountBalance = "";
  // String transactionDetail = "";
  final AccountStatementResponseModel statment;
  final Function()? onTap;

  const AccountStatementWidget({
    Key? key,
    required this.statment,
    this.onTap,
  }) : super(key: key);

  @override
  State<AccountStatementWidget> createState() => _AccountStatementWidgetState();
}

class _AccountStatementWidgetState extends State<AccountStatementWidget> {
  //

  AccountStatementController accountStatementController = Get.find<AccountStatementController>();

  bool isExpanded = false;

  bool isDr(AccountStatementResponseModel statment) {
    if (statment.drMbmBkmsnumber == accountStatementController.selectedDepositAccount.value.mbmBkmsnumber) {
      return true;
    } else {
      return false;
    }
  }

  IconData statementIcon(AccountStatementResponseModel statment) {
    if (statment.batAcnttranReversed) {
      return Icons.redo;
    } else if (statment.drMbmBkmsnumber == accountStatementController.selectedDepositAccount.value.mbmBkmsnumber) {
      return Icons.arrow_upward_outlined;
    } else {
      return Icons.arrow_downward_outlined;
    }
  }

  Color statementColor(AccountStatementResponseModel statment) {
    if (statment.batAcnttranReversed) {
      return AppColors.colorGrey400;
    } else if (statment.drMbmBkmsnumber == accountStatementController.selectedDepositAccount.value.mbmBkmsnumber) {
      return AppColors.red;
    } else {
      return AppColors.colorButton;
    }
  }
  @override
  Widget build(BuildContext context) {
    var statment = widget.statment;
    return InkWell(
      onTap: widget.onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 3.0, horizontal: 3.0),
        child: Card(
          color: Colors.white,
          elevation: 2,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5),
          ),
          child: ExpansionTile(
            dense: false,
            onExpansionChanged: (value) {
              // accountStatementController.toggleExpansion(value);
              isExpanded = value;
              setState(() {});
            },
            maintainState: true,
            //expandedAlignment: Alignment.topLeft,
            expandedCrossAxisAlignment: CrossAxisAlignment.start,
            childrenPadding: const EdgeInsets.only(left: 12, right: 10, bottom: 10),
            tilePadding: const EdgeInsets.all(8.0),
            collapsedShape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5),
            ),
            collapsedBackgroundColor: AppColors.white,
            shape: const Border(),
            backgroundColor: AppColors.white,
            expandedAlignment: Alignment.centerLeft,
            leading: SizedBox(
              height: 35,
              width: 35,
              child: CircleAvatar(backgroundColor: statementColor(statment) /*colorPrimary.withOpacity(0.7)*/, child: Padding(padding: EdgeInsets.all(2.0), child: Icon(color: AppColors.white, statementIcon(statment)))),
            ),
            trailing: isExpanded
                ? const SizedBox()
                : Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text("${statment.drPcrCurrshort} ${statment.sgtGntramt}", style: Theme.of(context).textTheme.titleSmall),
                      const SizedBox(height: 5),
                      Text("${isDr(statment) ? statment.drPcrCurrshort : statment.crPcrCurrshort} ${isDr(statment) ? statment.drMbmBkmsbalance : statment.crMbmBkmsbalance}", style: Theme.of(context).textTheme.bodySmall),
                    ],
                  ),
            title: isExpanded
                ? const SizedBox()
                : Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(isDr(statment) ? statment.crMbmBkmstitle.toString() : statment.drmbmBkmstitle.toString(), style: Theme.of(context).textTheme.titleSmall),
                      const SizedBox(height: 5),
                      Text(statment.drSgtGntrdate.toString().isEmpty ? "0" : statment.drSgtGntrdate.toString(), style: Theme.of(context).textTheme.bodySmall),
                    ],
                  ),
            children: [
              Text("Amount: ${statment.drPcrCurrshort} ${statment.sgtGntramt.toString()}", style: Theme.of(context).textTheme.labelMedium!.copyWith(fontSize: 14, color: AppColors.colorTitle)),
              const SizedBox(height: 5),
              Text("Transaction-ID: ${statment.sgtSentGntrnumber.toString()}${statment.sgtSentNodeId}", style: Theme.of(context).textTheme.labelSmall),
              const SizedBox(height: 5),
              Text("Date: ${statment.drSgtGntrdate.toString().isEmpty ? "0" : statment.drSgtGntrdate.toString()}", style: Theme.of(context).textTheme.labelSmall),
              const SizedBox(height: 5),
              Text("${isDr(statment) ? "Receiver: " : "Sender: "} ${isDr(statment) ? statment.crMbmBkmstitle : statment.drmbmBkmstitle}", style: Theme.of(context).textTheme.bodySmall),
              const SizedBox(height: 3),
              Text("Running-Balance: ${isDr(statment) ? statment.drPcrCurrshort : statment.crPcrCurrshort} ${isDr(statment) ? statment.drMbmBkmsbalance : statment.crMbmBkmsbalance}", style: Theme.of(context).textTheme.labelMedium!.copyWith(color: AppColors.colorTitle)),
            ],
          ),
        ),
      ),
    );
  }
}
