import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/views/ai/info_state.dart';
import 'package:didvan/views/widgets/didvan/button.dart';
import 'package:didvan/views/widgets/didvan/divider.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/hoshan_app_bar.dart';
import 'package:didvan/views/widgets/state_handlers/empty_connection.dart';
import 'package:didvan/views/widgets/video/chat_video_player.dart';
import 'package:didvan/views/widgets/video/primary_controls.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher_string.dart';

class InfoPage extends StatefulWidget {
  const InfoPage({Key? key}) : super(key: key);

  @override
  State<InfoPage> createState() => _InfoPageState();
}

class _InfoPageState extends State<InfoPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HoshanAppBar(
        withActions: false,
        withInfo: false,
        onBack: () => Navigator.pop(context),
      ),
      body: Consumer<InfoState>(builder: (context, state, child) {
        if (state.appState == AppState.busy) {
          return Center(
            child: SpinKitThreeBounce(
              size: 46,
              color: Theme.of(context).colorScheme.primary,
            ),
          );
        }
        if (state.appState == AppState.failed) {
          return Center(
            child: EmptyConnection(onRetry: state.getTools),
          );
        }
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: SingleChildScrollView(
                physics: const BouncingScrollPhysics(),
                child: Column(
                  children: [
                    if (state.infoModel.title != null)
                      Center(
                        child: Padding(
                          padding: const EdgeInsets.only(top: 32, bottom: 24),
                          child: DidvanText(
                            state.infoModel.title!,
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: Theme.of(context).colorScheme.checkFav,
                          ),
                        ),
                      ),
                    if (state.infoModel.url != null)
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 20.0),
                        child: ClipRRect(
                          borderRadius: DesignConfig.lowBorderRadius,
                          child: ChatVideoPlayer(
                            src: state.infoModel.url!,
                            showOptions: true,
                            custome: const PrimaryControls(),
                          ),
                        ),
                      ),
                    if (state.infoModel.url != null)
                      Padding(
                        padding: const EdgeInsets.all(20.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            const Row(
                              children: [
                                DidvanText(
                                  'آنچه در این ویدیو خواهید دید:',
                                  fontSize: 16,
                                ),
                              ],
                            ),
                            Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: ListView.builder(
                                  physics: const NeverScrollableScrollPhysics(),
                                  shrinkWrap: true,
                                  itemCount:
                                      state.infoModel.description!.length,
                                  itemBuilder: (context, index) => summery(
                                      state.infoModel.description?[index] ??
                                          ''),
                                ))
                          ],
                        ),
                      ),
                    if (state.infoModel.pdf != null)
                      Padding(
                        padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
                        child: DidvanButton(
                          title: 'دانلود فایل راهنما',
                          onPressed: () {
                            launchUrlString(state.infoModel.pdf!,
                                mode: LaunchMode.inAppBrowserView);
                          },
                        ),
                      ),
                  ],
                ),
              ),
            ),
            Column(
              children: [
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 20.0),
                  child: DidvanDivider(
                    verticalPadding: 12,
                    height: 4,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Icon(
                      DidvanIcons.support_solid,
                      size: 32,
                    ),
                    const SizedBox(
                      width: 8,
                    ),
                    const DidvanText('هنوز سوالی دارید؟'),
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).pushNamed(
                          Routes.direct,
                          arguments: {'type': 'پشتیبانی اپلیکیشن'},
                        );
                      },
                      child: const DidvanText(
                        ' پیام به پشتیبانی',
                        color: Color(0xff007EA7),
                      ),
                    )
                  ],
                ),
                const SizedBox(
                  height: 32,
                ),
              ],
            )
          ],
        );
      }),
    );
  }

  Column summery(String text) {
    return Column(
      children: [
        Row(
          children: [
            Container(
              margin: const EdgeInsets.only(left: 8),
              decoration: const ShapeDecoration(
                shape: CircleBorder(
                    side: BorderSide(width: 3, color: Colors.black)),
              ),
            ),
            DidvanText(
              text,
              fontSize: 14,
            ),
          ],
        ),
        const SizedBox(
          height: 4,
        ),
      ],
    );
  }
}
