import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/GeneratorDetailsProvider.dart';
import 'package:generp/screens/genTracker/ComplaintHistory.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../Notifiers/HomeScreenNotifier.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';

class Generatordetails extends StatefulWidget {
  final activityName;
  final genLocation;
  final generatorId;

  const Generatordetails({
    super.key,
    this.activityName,
    this.genLocation,
    required this.generatorId,
  });

  @override
  State<Generatordetails> createState() => _GeneratordetailsState();
}

class _GeneratordetailsState extends State<Generatordetails> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var details = Provider.of<Generatordetailsprovider>(
        context,
        listen: false,
      );
      var homeProvider = Provider.of<HomescreenNotifier>(
        context,
        listen: false,
      );
      if (widget.activityName == "NearByGenerators") {
        details.LoadTechniciangeneratorDetailsApifunction(
          homeProvider,
          context,
          "",
          widget.generatorId,
        );
      } else {
        details.LoadgeneratorDetailsApifunction(
          homeProvider,
          context,
          "",
          widget.generatorId,
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Generatordetailsprovider>(
      builder: (context, provider, child) {
        var sendwidget = Container(
          padding: EdgeInsets.only(right: 10.0),
          child: InkWell(
            onTap: () async {
              if (widget.genLocation != null) {
                var loc = widget.genLocation?.split(",").toString();

                var uri = Uri.parse(
                  "google.navigation:q=${loc![0]},${loc![1]}&mode=d",
                );
                if (Platform.isIOS) {
                  uri = Uri.parse(
                    "maps://?q=${loc![0]},${loc![1]}&z=10&daddr=${loc![0]},${loc![1]}&dirflg=d",
                  );
                }
                if (await canLaunch(uri.toString())) {
                  await launch(uri.toString());
                } else {
                  throw 'Could not launch ${uri.toString()}';
                }
                // val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
              }
            },
            child: SvgPicture.asset("assets/svg/directions_ic.svg"),
          ),
        );
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar:
                widget.activityName == "NearByGenerators"
                    ? appbar2(context, "Generator Details",provider.resetForm, sendwidget)
                    : appbar(context, "Generator Details"),
            backgroundColor: AppColors.scaffold_bg_color,
            body: SafeArea(
              child: Container(
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
                        child: Text(
                          "Customer Details",
                          style: TextStyle(color: Color(0xFF818181),
                          fontFamily: "JakartaMedium"),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                        padding: EdgeInsets.symmetric(
                          horizontal: 15,
                          vertical: 10,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Column(
                          children: [
                            Container(
                              padding: EdgeInsets.symmetric(
                                vertical: 3,
                                horizontal: 0,
                              ),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
              
                                  SvgPicture.asset(
                                    "assets/svg/se_block_head.svg",
                                  ),
                                  SizedBox(width: 5),
              
                                  Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(
                                        width:
                                        MediaQuery.of(
                                          context,
                                        ).size.width *
                                            0.75,
                                        child: Text(
                                          "${provider.cname}",
                                          maxLines: 2,
                                          overflow:
                                          TextOverflow.ellipsis,
                                          style: TextStyle(
                                            fontFamily: "JakartaMedium",
                                            fontSize: 14
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                                        // height:45,
                                        width:
                                        MediaQuery.of(
                                          context,
                                        ).size.width *
                                            0.75,
                                        child: Text(
                                          "${provider.aname}",
                                          maxLines: 2,
                                          overflow:
                                          TextOverflow.ellipsis,
                                          style: TextStyle(
                                            fontSize: 14,
                                            color: Color(
                                              0xFF818181,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: List.generate(3, (j) {
                                final textheads = [
                                  "Mobile Number",
                                  "Contact Person Number",
                                  "Mail ID",
                                ];
                                final textSubheads = [
                                  "${provider.mob1}",
                                  "${provider.mob2}",
                                  "${provider.mail}",
                                ];
                                return Container(
                                  padding: EdgeInsets.symmetric(
                                    vertical: 6,
                                    horizontal: 0,
                                  ),
                                  child: Row(
              
                                    children: [
              
                                      Expanded(
                                        flex:1,
                                        child: SizedBox(
              
                                          child: Text(
                                            textheads[j].toString(),
                                            maxLines: 2,
                                            overflow:
                                                TextOverflow.ellipsis,
                                            style: TextStyle(
                                              fontFamily: "JakartaMedium",
                                              fontSize: 14
                                            ),
                                          ),
                                        ),
                                      ),
                                      SizedBox(width: 5,),
                                      Expanded(
                                        flex: 2,
                                        child: SizedBox(
              
                                          child: Text(
                                            textSubheads[j],
                                            maxLines: 2,
                                            overflow:
                                                TextOverflow.ellipsis,
                                            style: TextStyle(
                                              fontSize: 14,
                                              color: Color(
                                                0xFF818181,
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                );
                              }),
                            ),
                          ],
                        ),
                      ),
              
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
                        child: Text(
                          "Generator Details",
                          style: TextStyle(color: Color(0xFF818181),
                              fontFamily: "JakartaMedium"),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                        padding: EdgeInsets.symmetric(
                          horizontal: 15,
                          vertical: 10,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: List.generate(6, (j) {
                            final textheads = [
                              "Gen ID",
                              "Engine Number",
                              "Address",
                              "Product Name",
                              "Engine Model",
                              "Date Of Supply",
                            ];
                            final textSubheads = [
                              "${provider.genID}",
                              "${provider.engineNo}",
                              "${provider.address}",
                              "${provider.spname}",
                              "${provider.emodel}",
                              "${provider.dateOfEngineSale}",
                            ];
                            return Container(
                              padding: EdgeInsets.symmetric(
                                vertical: 6,
                                horizontal: 0,
                              ),
                              child: Row(
                                crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                children: [
                                  Expanded(
                                      flex:1,
                                      child: Text(textheads[j].toString(),style: TextStyle(
                                        fontSize: 14,
                                        fontFamily: "JakartaMedium"
                                      ),),),
                                  SizedBox(width: 5,),
                                  Expanded(
                                    flex:2,
                                    child: SizedBox(
              
                                      child: Text(
                                        textSubheads[j],
                                        maxLines: 2,
                                        overflow:
                                            TextOverflow.ellipsis,
                                        style: TextStyle(
                                          fontSize: 14,
                                          color: Color(
                                            0xFF818181,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            );
                          }),
                        ),
                      ),
              
                      SizedBox(height: 75),
                    ],
                  ),
                ),
              ),
            ),
            floatingActionButton: Align(
              alignment: Alignment.bottomCenter,
              child: InkWell(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => Complainthistory()),
                  );
                },
                child: Container(
                  alignment: Alignment.bottomCenter,
                  height: 45,
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                    color: AppColors.app_blue,
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: Center(
                    child: Text(
                      "Complaint History",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontFamily: "JakartaMedium",
                          fontSize: 15,
                          color: Colors.white),
                    ),
                  ),
                ),
              ),
            ),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerFloat,
          ),
        );
      },
    );
  }
}
