import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/new_models/work_order_detail_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/service_request_latest/request_detail_provider.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';

class HistoryLogView extends StatelessWidget {
  const HistoryLogView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<RequestDetailProvider>(builder: (context, requestProvider, _) {
      String status = "";
      return requestProvider.isLoading
          ? const CircularProgressIndicator(color: AppColor.primary10).center
          : (requestProvider.currentWorkOrder == null || (requestProvider.currentWorkOrder?.data?.workOrderHistory.isEmpty ?? true)
              ? const NoDataFound()
              : Stack(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(left: 122.toScreenWidth),
                      child: const VerticalDivider(
                        color: AppColor.white40,
                        thickness: 2,
                        width: 20,
                        indent: 10,
                        endIndent: 10,
                      ),
                    ),
                    Builder(builder: (cxt) {
                      List<Widget> items = [];
                      String lastStatus = "";
                      for (var object in requestProvider.currentWorkOrder!.data!.workOrderHistory) {
                        String currentStatus = object.workorderStatus!.name!;
                        if (lastStatus.isEmpty) {
                          lastStatus = currentStatus;
                        }
                        if (lastStatus == currentStatus) {
                          items.add(historyLogCard(cxt, object));
                        } else {
                          items.add(statusCard(cxt, lastStatus));
                          lastStatus = currentStatus;
                          items.add(historyLogCard(cxt, object));
                        }
                      }
                      items.add(statusCard(cxt, lastStatus));

                      return SingleChildScrollView(
                        padding: const EdgeInsets.all(16),
                        child: Column(children: items),
                      );
                    }),
                  ],
                ));
    });
  }

  Widget historyLogCard(BuildContext context, WorkOrderHistory object) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Align(
          alignment: Alignment.centerLeft,
          child: Text(
            object.date.toString().toServiceRequestCardFormat,
            textAlign: TextAlign.end,
            style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral120),
          ),
        ).expanded,
        Expanded(
          flex: 2,
          child: object.step!.name!.bodyText(context).custom(color: AppColor.black10),
        ),
      ],
    ).toShadowContainer(context, padding: 16).paddingOnly(bottom: 16);
  }

  Widget statusCard(BuildContext context, String status) {
    return SizedBox(
      width: 153.toScreenWidth,
      child: StatusLabel(
        radius: 4,
        label: statusText(status.toLowerCase()),
        labelInCenter: true,
        textColor: AppColor.getHistoryLogStatusTextColorByName(status),
        backgroundColor: AppColor.getHistoryLogStatusColorByName(status),
      ),
    ).paddingOnly(bottom: 16, start: 40);
  }

  String statusText(String status) {
    if (status == "new") {
      return "Open Request".addTranslation;
    } else if (status == "in progress") {
      return "Status update to In Progress".addTranslation;
    } else if (status == "closed") {
      return "Status update to Closed".addTranslation;
    } else if (status == "completed") {
      return "Status update to Completed".addTranslation;
    } else if (status == "fixed") {
      return "Status update to Fixed".addTranslation;
    } else if (status == "reject") {
      return "Status update to Rejected".addTranslation;
    } else
      return status;
  }
}

class HistoryLogModel {
  String message;
  DateTime date;
  String? status;
  Color? statusColor;

  HistoryLogModel({required this.message, required this.date, this.status, this.statusColor});
}
