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/modules/asset_delivery_module/models/technical_inspection_asset_model.dart';
import 'package:test_sa/modules/asset_delivery_module/pages/technical_inpection/technical_inpection_asset_card_view.dart';
import 'package:test_sa/modules/asset_delivery_module/pages/technical_inpection/update_parent_asset_view.dart';
import 'package:test_sa/modules/asset_delivery_module/provider/asset_delivery_provider.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';
import '../../../../new_views/common_widgets/default_app_bar.dart';

class TechnicalInspectionParentAssetListView extends StatefulWidget {
  final int? costCenterId;
  final int? tableItemId;
  final int? lineId;

  TechnicalInspectionParentAssetListView({
    Key? key,
    this.costCenterId,
    this.tableItemId,
    this.lineId,
  }) : super(key: key);

  @override
  State<TechnicalInspectionParentAssetListView> createState() => _TechnicalInspectionParentAssetListViewState();
}

class _TechnicalInspectionParentAssetListViewState extends State<TechnicalInspectionParentAssetListView> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  bool viewOnly = false;
  AssetDeliveryProvider? assetDeliveryProvider;
  List<TechnicalInspectionAssetModel> assetList = [];
  bool isLoading = false;

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getInitialData();
    });
    super.initState();
  }

  void getInitialData() async {
    setState(() {
      isLoading = true;
    });
    assetDeliveryProvider = Provider.of<AssetDeliveryProvider>(context, listen: false);
    //TODO replace static id's after testing...
    assetList = await assetDeliveryProvider!.getParentAssetList(
      costCenterId: widget.costCenterId,
      tableItemId: widget.tableItemId,
      lineId: widget.lineId,
    );

    setState(() {
      isLoading = false;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: DefaultAppBar(
        title: 'Technical Inspection'.addTranslation,
        titleStyle: AppTextStyles.heading3.copyWith(fontWeight: FontWeight.w500, color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
      ),
      body:Form(
        key: _formKey,
        child: parentAssetList(context), // parentAssetList already returns a ListView
      ),
    );
  }

  Widget parentAssetList(BuildContext context) {
    if (isLoading) {
      return SizedBox.expand(
        child: CircularProgressIndicator(color: AppColor.loadingColor(context)).center,
      );
    }

    if (assetList.isEmpty) {
      return const NoDataFound().center;
    }

    return ListView.separated(
      padding: const EdgeInsets.all(16),
      itemCount: assetList.length + 1, // +1 for the "Parents" header
      separatorBuilder: (_, __) => 8.height,
      itemBuilder: (context, index) {
        if (index == 0) {
          return Text(
            "Parents",
            style: AppTextStyles.heading6.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.black10),
          );
        }

        final asset = assetList[index - 1];
        return TechnicalInspectionAssetCard(
          assetModel: asset,
          isViewOnly: viewOnly,
          editPress: () async {
            bool isRefreshed = await Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => UpdateParentAssetView(
                  assetModel: asset,
                  lineId: widget.lineId,
                  tableItemId: widget.tableItemId,
                  costCenterItemId: widget.costCenterId,
                ),
              ),
            );

            if (isRefreshed) {
              getInitialData();
            }
          },
        );
      },
    );
  }
}
