import 'package:double_back_to_close_app/double_back_to_close_app.dart';
import 'package:IQ/app/global/text_widget.dart';
import 'package:IQ/app/modules/home/providers/user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import 'package:get/get.dart';
import 'package:IQ/app/global/global_snackbar.dart';
import 'package:IQ/app/global/static_informs.dart';
import 'package:IQ/app/modules/drawerSide/views/drawer_side_view.dart';

import '../controllers/wifi_test_controller.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class WifiTestView extends GetView<WifiTestController> {
  WifiTestView({Key? key}) : super(key: key);
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection:
          defaultLocale != "en" ? TextDirection.ltr : TextDirection.rtl,
      child: Scaffold(
        backgroundColor: Colors.white,
        drawerScrimColor: Colors.black.withOpacity(0.7),
        key: scaffoldKey,
        endDrawer: const DrawerSideView(),
        drawerEdgeDragWidth: widthSize(context),
        body: DoubleBackToCloseApp(
          snackBar: snackBar,
          child: Stack(
            children: [
              Container(
                margin: const EdgeInsets.only(top: 250),
                child: Stack(
                  children: [
                    GetBuilder(
                      builder: (WifiTestController c) {
                        return InAppWebView(
                          ///
                          key: c.webViewKey,
                          initialUrlRequest: URLRequest(
                            url: Uri.parse("https://www.speedtest.net/"),
                          ),
                          initialOptions: InAppWebViewGroupOptions(
                            crossPlatform: InAppWebViewOptions(
                              useShouldOverrideUrlLoading: true,
                              mediaPlaybackRequiresUserGesture: false,
                            ),
                            android: AndroidInAppWebViewOptions(
                              useHybridComposition: true,
                            ),
                            ios: IOSInAppWebViewOptions(
                              allowsInlineMediaPlayback: true,
                            ),
                          ),

                          ///
                          pullToRefreshController: c.pullToRefreshController,
                          onLoadStart: controller.onLoadStart,
                          onLoadStop: controller.onLoadStop,
                          onLoadError: controller.onLoadError,

                          ///
                          onWebViewCreated: controller.onWebViewCreated,
                          onProgressChanged: controller.onProgressChanged,

                          ///
                          androidOnPermissionRequest: (_, __, resources) async {
                            return PermissionRequestResponse(
                              resources: resources,
                              action: PermissionRequestResponseAction.GRANT,
                            );
                          },
                          onConsoleMessage: (controller, consoleMessage) {
                            debugPrint('consoleMessage = $consoleMessage');
                          },

                          ///
                          shouldOverrideUrlLoading:
                              controller.shouldOverrideUrlLoading,
                        );
                      },
                    ),
                    GetBuilder(
                      builder: (WifiTestController co) {
                        return co.progress1 < 1.0
                            ? LinearProgressIndicator(value: co.progress1)
                            : Container();
                      },
                    ),
                  ],
                ),
              ),
              Container(
                height: 250,
                decoration: BoxDecoration(
                  color: brandColor,
                  image: const DecorationImage(
                    image: AssetImage('assets/background.png'),
                    fit: BoxFit.fill,
                  ),
                ),
                child: Stack(
                  children: [
                    Container(
                      height: 230,
                      decoration: BoxDecoration(
                        color: brandColor,
                        image: const DecorationImage(
                          image: AssetImage('assets/background.png'),
                          fit: BoxFit.fill,
                        ),
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          SizedBox(
                              height: MediaQuery.of(context).viewPadding.top),
                          Container(
                            margin: const EdgeInsets.symmetric(
                              horizontal: 15,
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Container(
                                  margin: const EdgeInsets.only(
                                    top: 5,
                                    right: 15,
                                  ),
                                  child: Row(
                                    children: [
                                      SvgPicture.asset(
                                        "assets/2/icon.svg",
                                        height: 20,
                                        width: 71.16,
                                      ),
                                      const SizedBox(width: 10),
                                      Image.asset("assets/logo.png"),
                                    ],
                                  ),
                                ),
                                const SizedBox(width: 10),
                                Row(
                                  children: [
                                    Text(
                                      user?.data?.firstname ?? ' . . . ',
                                      style: const TextStyle(
                                        fontSize: 20,
                                        color: Colors.white,
                                        fontWeight: FontWeight.normal,
                                      ),
                                    ),
                                    IconButton(
                                      onPressed: () {
                                        scaffoldKey.currentState
                                            ?.openEndDrawer();
                                      },
                                      icon: const Icon(
                                        Icons.menu,
                                        color: Colors.white,
                                        size: 40,
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          Container(
                            alignment: Alignment.center,
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                const SizedBox(height: 30),
                                SvgPicture.asset(
                                  'assets/speedometer.svg',
                                  height: 50,
                                  width: 50,
                                  color: Colors.white,
                                ),
                                const SizedBox(height: 20),
                                const TextWidget(
                                  text: "SpeedTest",
                                  fontSize: 28,
                                  color: Colors.white,
                                  fontWeight: FontWeight.w600,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
