import 'package:SEFER/controller/functions/tts.dart';
import 'package:SEFER/views/widgets/elevated_btn.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/constant/style.dart';
import 'package:SEFER/views/lang/languages.dart';
import 'package:SEFER/views/widgets/my_scafold.dart';
import 'package:path/path.dart' as path;
import 'package:share/share.dart';

import '../../controller/functions/audio_record1.dart';
import 'profile/passenger_profile_page.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      isleading: true,
      title: 'Home Page'.tr,
      body: [
        Column(
          children: [
            ListTile(
              onTap: () {
                Get.to(() => const Language());
              },
              title: Text(
                'Language'.tr,
                style: AppStyle.headTitle2,
              ),
              subtitle: Text('To change Language the App'.tr),
              trailing: const Icon(
                Icons.arrow_forward_ios,
                size: 30,
                color: AppColor.primaryColor,
              ),
              leading: const Icon(
                Icons.language_sharp,
                color: AppColor.primaryColor,
              ),
            ),
            ListTile(
              leading: const Icon(Icons.location_city_outlined),
              title: Text(
                'Change Country'.tr,
                style: AppStyle.headTitle2,
              ),
              subtitle:
                  Text('You can change the Country to get all features'.tr),
              onTap: () => Get.to(MyScafolld(
                  title: 'Change Country'.tr,
                  body: [CountryPickerFromSetting()],
                  isleading: true)),
            ),
            ListTile(
                leading: const Icon(Icons.record_voice_over_outlined),
                title: Text(
                  'Trips recorded'.tr,
                  style: AppStyle.headTitle2,
                ),
                subtitle: Text('Here recorded trips audio'.tr),
                onTap: () async {
                  Get.defaultDialog(
                    title: 'Select recorded trip'.tr,
                    titleStyle: AppStyle.title,
                    content:
                        GetBuilder<AudioRecorderController>(builder: (audio) {
                      return Column(
                        children: [
                          FutureBuilder<List<String>>(
                            future: audio.getRecordedFiles(),
                            builder: (context, snapshot) {
                              if (snapshot.connectionState ==
                                  ConnectionState.waiting) {
                                return const CircularProgressIndicator();
                              } else if (snapshot.hasData) {
                                final recordedFiles = snapshot.data!;
                                return DropdownButton<String>(
                                  value: audio.selectedFilePath,
                                  onChanged: (value) {
                                    audio.selectedFilePath = value;
                                    audio.playRecordedFile(value!);
                                    audio.update();
                                  },
                                  items: recordedFiles
                                      .map((file) => DropdownMenuItem<String>(
                                            value: file,
                                            child: Text(path.basename(file)),
                                          ))
                                      .toList(),
                                );
                              } else {
                                return Text('Error: ${snapshot.error}');
                              }
                            },
                          ),
                          Slider(
                            value: audio.currentPosition,
                            max: audio.totalDuration,
                            inactiveColor: AppColor.accentColor,
                            label: audio.currentPosition.toString(),
                            onChanged: (value) {
                              audio.currentPosition = value;
                              // audio.update();
                              audio.audioPlayer
                                  .seek(Duration(seconds: value.toInt()));
                            },
                          ),
                          Align(
                            alignment: Alignment.bottomCenter,
                            child: Container(
                              padding: const EdgeInsets.all(16.0),
                              color: Colors.grey[200],
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    audio.selectedFilePath != null
                                        ? '${'Selected file:'.tr} ${path.basename(audio.selectedFilePath!)}'
                                        : 'No file selected'.tr,
                                    style: AppStyle.subtitle,
                                  ),
                                  if (audio.selectedFilePath != null)
                                    IconButton(
                                      icon: const Icon(Icons.share),
                                      onPressed: () {
                                        Share.shareFiles(
                                            [audio.selectedFilePath!]);
                                      },
                                    ),
                                ],
                              ),
                            ),
                          ),
                          IconButton(
                            onPressed: () async {
                              Get.defaultDialog(
                                  title: 'Are you sure to delete recorded files'
                                      .tr,
                                  content: Column(
                                    children: [
                                      IconButton(
                                        onPressed: () {
                                          Get.find<TextToSpeechController>()
                                              .speakText(
                                                  'this will delete all files from your device'
                                                      .tr);
                                        },
                                        icon: const Icon(Icons.headphones),
                                      ),
                                      Text(
                                        'this will delete all files from your device'
                                            .tr,
                                        textAlign: TextAlign.center,
                                        style: AppStyle.title,
                                      ),
                                    ],
                                  ),
                                  titleStyle: AppStyle.title,
                                  confirm: MyElevatedButton(
                                      title: 'Delete'.tr,
                                      kolor: AppColor.redColor,
                                      onPressed: () async {
                                        await audio
                                            // .deleteRecordedFile(audio.selectedFilePath!);
                                            .deleteAllRecordedFiles();
                                        Get.back();
                                        Get.back();
                                      }));
                            },
                            icon: const Icon(Icons.delete),
                          ),
                        ],
                      );
                    }),
                  );
                }),
          ],
        ),
      ],
    );
  }
}
