import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/pages/user/requests/report/create_service_report.dart';
import 'package:test_sa/views/widgets/buttons/app_back_button.dart';
import 'package:test_sa/views/widgets/buttons/app_button.dart';
import 'package:test_sa/views/widgets/buttons/app_icon_button.dart';
import 'package:test_sa/views/widgets/images/images_list.dart';
import 'package:test_sa/views/widgets/loaders/image_loader.dart';
import 'package:test_sa/views/widgets/requests/info_row.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';
import 'package:test_sa/views/widgets/requests/service_request_update_dialog.dart';
import 'package:test_sa/views/widgets/sound/sound_player.dart';
import 'package:test_sa/views/widgets/titles/app_sub_title.dart';

import 'report/future_service_report.dart';
class RequestDetailsPage extends StatelessWidget {
  static final String id = "/call-details";
  final ServiceRequest serviceRequest;

  const RequestDetailsPage({Key key, this.serviceRequest}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    Subtitle _subtitle = AppLocalization.of(context).subtitle;
    UserProvider _userProvider = Provider.of<UserProvider>(context);
    SettingProvider _settingProvider = Provider.of<SettingProvider>(context);
    ServiceRequestsProvider _serviceRequestsProvider = Provider.of<ServiceRequestsProvider>(context);
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        body: SafeArea(
          child: Column(
            children: [
              Container(
                color:AColors.primaryColor,
                padding: const EdgeInsets.symmetric(horizontal: 0,vertical: 4),
                child: Row(
                  children: [
                    ABackButton(),
                    Expanded(
                      child: Center(
                        child: Text(
                          _subtitle.details,
                          style: Theme.of(context).textTheme.headline6.copyWith(
                              color: AColors.white,
                              fontStyle: FontStyle.italic
                          ),
                        ),
                      ),
                    ),
                    _userProvider.user.type == UsersTypes.normal_user ?
                    // AIconButton(
                    //   iconData: Icons.warning_amber_rounded,
                    //   color: AColors.white,
                    //   buttonSize: 42,
                    //   backgroundColor: AColors.deepOrange,
                    //   onPressed: (){
                    //     Navigator.of(context).push(
                    //         MaterialPageRoute(
                    //             builder: (_) => ReportIssuesPage(serviceRequest: serviceRequest,)
                    //         )
                    //     );
                    //   },
                    // )
                    const SizedBox(width: 48,)
                        : AIconButton(
                      iconData: Icons.edit,
                      color: AColors.white,
                      buttonSize: 42,
                      backgroundColor: AColors.green,
                      onPressed: () async {
                        showModalBottomSheet(
                            context: context,
                            builder: (context){
                              return ServiceRequestsUpdateDialog(request: serviceRequest,);
                            });
                        // DateTime picked = await showDatePicker(
                        //     context: context,
                        //     initialDate: DateTime.now(),
                        //     firstDate: DateTime.now(),
                        //     lastDate: DateTime.now().add(Duration(days: 182))
                        // );
                        // if(picked == null){return;}
                        // showDialog<void>(
                        //   context: context,
                        //   barrierDismissible: false,
                        //   builder: (BuildContext context) {
                        //     return CupertinoAlertDialog(
                        //       title: Text(_subtitle.updatingDots),
                        //       content: Center(child: CircularProgressIndicator()),
                        //     );
                        //   },
                        // );
                        // int status = await _serviceRequestsProvider.updateDate(
                        //     user: _userProvider.user,
                        //     host: _settingProvider.host,
                        //     request: serviceRequest,
                        //     newDate: picked.toString().split(" ").first
                        // );
                        // Navigator.of(context).pop();
                        // Fluttertoast.showToast(
                        //   msg: HttpStatusManger.getStatusMessage(status: status, subtitle: _subtitle),
                        // );
                      },
                    ),
                    SizedBox(width: 16,)
                  ],
                ),
              ),

              serviceRequest.devicePhotos.isEmpty ? SizedBox.shrink():
              Column(
                children: [
                  SizedBox(height: 8,),
                  MaterialButton(
                    padding: EdgeInsets.zero,
                    onPressed: (){
                      Navigator.of(context).push(
                          MaterialPageRoute(
                              builder: (_) => Scaffold(
                                body: InteractiveViewer(
                                  child: Center(
                                    child: ImageLoader(
                                      url: serviceRequest.devicePhotos.first,
                                      boxFit: BoxFit.contain,
                                    ),
                                  ),
                                ),
                              )
                          )
                      );
                    },
                    child: SizedBox(
                      height: 140 * AppStyle.getScaleFactor(context),
                      width: MediaQuery.of(context).size.width,
                      child: ImageLoader(
                        url: serviceRequest.devicePhotos.first,
                        boxFit: BoxFit.cover,
                      ),
                    ),
                  ),
                  SizedBox(height: 8,),
                  SizedBox(
                    height: 60* AppStyle.getScaleFactor(context),
                    child: ImagesList(
                      images: serviceRequest.devicePhotos,
                    ),
                  ),
                ],
              ),

              TabBar(
                labelColor: AColors.primaryColor,
                tabs: [
                Tab(text: _subtitle.general,),
                Tab(text: _subtitle.serviceRequestInformation,),
              ]),
              SizedBox(height: 8,),
              Expanded(
                child: TabBarView(

                  children: [
                    ListView(
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      children: [
                        RequestInfoRow(
                          title: _subtitle.code,
                          info: serviceRequest.requestCode,
                        ),
                        RequestInfoRow(
                          title: "Asset Number",
                          info: serviceRequest.deviceNumber,
                        ),
                        RequestInfoRow(
                          title: _subtitle.deviceSN,
                          info: serviceRequest.deviceSerialNumber,
                        ),
                        RequestInfoRow(
                          title: _subtitle.deviceModel,
                          info  : serviceRequest.deviceModel,
                        ),
                        RequestInfoRow(
                          title: _subtitle.engineerName,
                          info: serviceRequest.engineerName,
                        ),
                        RequestInfoRow(
                          title: _subtitle.engineerPhone,
                          info: serviceRequest.engineerMobile,
                        ),
                        RequestInfoRow(
                          title: _subtitle.date,
                          info: serviceRequest.date,
                        ),
                        serviceRequest.nextVisitDate == null ? SizedBox.shrink() :
                        RequestInfoRow(
                          title: _subtitle.nextVisitDate,
                          info: DateFormat('EE dd/MM/yyyy').format(serviceRequest.nextVisitDate),
                        ),
                        Row(
                          children: [
                            Expanded(
                              child: Text(
                                "${_subtitle.status} :  ",
                                style: Theme.of(context).textTheme.subtitle1,
                                textScaleFactor: AppStyle.getScaleFactor(context),
                              ),
                            ),
                            StatusLabel(label: serviceRequest.statusLabel,
                                color: AColors.getRequestStatusColor(serviceRequest.statusValue)
                            ),
                          ],
                        ),
                        Divider(color: Theme.of(context).primaryColor,),
                        RequestInfoRow(
                          title: _subtitle.hospital,
                          info: serviceRequest.hospitalName,
                        ),
                        RequestInfoRow(
                          title: _subtitle.unite,
                          info: serviceRequest.departmentName,
                        ),
                        // RequestInfoRow(
                        //   title: _subtitle.deviceArName,
                        //   content: serviceRequest.deviceArName,
                        // ),
                        RequestInfoRow(
                          title: _subtitle.deviceEnName,
                          content: serviceRequest.deviceEnName,
                        ),
                        RequestInfoRow(
                          title: _subtitle.maintenanceIssue,
                          content: serviceRequest.maintenanceIssue,
                        ),
                        if(serviceRequest.audio?.isNotEmpty == true)
                        ASoundPlayer(
                          audio: serviceRequest.audio,
                        ),
                        //
                        // Center(
                        //   child: Padding(
                        //     padding: EdgeInsets.all(32),
                        //     child: AButton(
                        //       text: _subtitle.duplicateRequest,
                        //       onPressed: () async {
                        //         bool result = await showDialog(
                        //             context: context,
                        //             builder: (_) => AAlertDialog(
                        //               title: _subtitle.duplicateAlert,
                        //               content: _subtitle.duplicateAlertMessage,
                        //             )
                        //         );
                        //         if(result == true){
                        //           showDialog(
                        //               context: context,
                        //               builder: (context){
                        //                 return Center(child: CircularProgressIndicator());
                        //               }
                        //           );
                        //           int status = await _serviceRequestsProvider.createDuplicatedReport(
                        //               host: _settingProvider.host,
                        //               user: _userProvider.user,
                        //               request: serviceRequest
                        //           );
                        //           Navigator.of(context).pop();
                        //           ScaffoldMessenger.of(context).showSnackBar(
                        //               SnackBar(
                        //                 content: Text(
                        //                   HttpStatusManger.getStatusMessage(status: status, subtitle: _subtitle)
                        //                 )
                        //               )
                        //           );
                        //         }
                        //       },
                        //     ),
                        //   ),
                        // )
                      ],
                    ),
                    serviceRequest.viewReport ?
                    ListView(
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      children: [
                        RequestInfoRow(
                          title: _subtitle.faultDescription,
                          content: serviceRequest.faultDescription,
                        ),
                        RequestInfoRow(
                          title: _subtitle.workPerformed,
                          content: serviceRequest.workPerformed,
                        ),
                        RequestInfoRow(
                          title: _subtitle.visitDate,
                          info: serviceRequest.visitDate,
                        ),
                        RequestInfoRow(
                          title: _subtitle.jobSheetNumber,
                          info: serviceRequest.jobSheetNumber,
                        ),
                        _userProvider.user.type == UsersTypes.engineer
                        && serviceRequest.reportID != null?
                        Padding(
                          padding: EdgeInsets.all(32),
                          child: AButton(
                            text: _subtitle.editServiceReport,
                            onPressed: (){
                              Navigator.of(context).push(
                                MaterialPageRoute(
                                    builder: (_) => FutureServiceReport(
                                      request: serviceRequest,
                                    )
                                ),
                              );
                            },
                          ),
                        ): SizedBox.shrink(),
                      ],
                    ):
                    _userProvider.user.type == UsersTypes.engineer ?
                    Center(
                      child: Padding(
                        padding: EdgeInsets.all(32),
                        child: AButton(
                          text: "Create Report",
                          onPressed: (){
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                  builder: (_) => CreateServiceReport(
                                    request: serviceRequest,
                                  )
                              ),
                            );
                          },
                        ),
                      ),
                    ): Center(child: ASubTitle(_subtitle.noDateFound),),


                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
