/* 
 * This file is part of Stack Wallet.
 * 
 * Copyright (c) 2023 Cypher Stack
 * All Rights Reserved.
 * The code is distributed under GPLv3 license, see LICENSE file for details.
 * Generated by Cypher Stack on 2023-05-26
 *
 */

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';

import '../app_config.dart';
import '../db/hive/db.dart';
import '../pages_desktop_specific/password/create_password_view.dart';
import '../providers/global/prefs_provider.dart';
import '../providers/global/price_provider.dart';
import '../services/exchange/exchange_data_loading_service.dart';
import '../themes/stack_colors.dart';
import '../themes/theme_providers.dart';
import '../utilities/assets.dart';
import '../utilities/constants.dart';
import '../utilities/text_styles.dart';
import '../utilities/util.dart';
import '../widgets/conditional_parent.dart';
import '../widgets/custom_buttons/app_bar_icon_button.dart';
import '../widgets/desktop/desktop_app_bar.dart';
import '../widgets/desktop/desktop_scaffold.dart';
import '../widgets/desktop/primary_button.dart';
import '../widgets/rounded_white_container.dart';
import 'pinpad_views/create_pin_view.dart';

class StackPrivacyCalls extends ConsumerStatefulWidget {
  const StackPrivacyCalls({
    super.key,
    required this.isSettings,
  });

  final bool isSettings;

  static const String routeName = "/stackPrivacy";

  @override
  ConsumerState<StackPrivacyCalls> createState() => _StackPrivacyCalls();
}

class _StackPrivacyCalls extends ConsumerState<StackPrivacyCalls> {
  late final bool isDesktop;
  late bool isEasy;
  late bool infoToggle;

  @override
  void initState() {
    isDesktop = Util.isDesktop;
    isEasy = ref.read(prefsChangeNotifierProvider).externalCalls;
    infoToggle = isEasy;
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MasterScaffold(
      background: Theme.of(context).extension<StackColors>()!.background,
      isDesktop: isDesktop,
      appBar: isDesktop
          ? const DesktopAppBar(
              isCompactHeight: false,
              leading: AppBarBackButton(),
            )
          : AppBar(
              leading: AppBarBackButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ),
      body: SafeArea(
        child: ConditionalParent(
          condition: !isDesktop,
          builder: (child) => LayoutBuilder(
            builder: (context, constraints) => SingleChildScrollView(
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  minHeight: constraints.maxHeight,
                ),
                child: IntrinsicHeight(
                  child: child,
                ),
              ),
            ),
          ),
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, isDesktop ? 0 : 40, 0, 0),
            child: ConstrainedBox(
              constraints: BoxConstraints(
                maxWidth: isDesktop ? 480 : double.infinity,
              ),
              child: ConditionalParent(
                condition: isDesktop,
                builder: (child) => SingleChildScrollView(
                  child: child,
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "Choose your ${AppConfig.prefix} experience",
                      textAlign: TextAlign.center,
                      style: isDesktop
                          ? STextStyles.desktopH2(context)
                          : STextStyles.pageTitleH1(context),
                    ),
                    SizedBox(
                      height: isDesktop ? 16 : 8,
                    ),
                    Text(
                      !widget.isSettings
                          ? "You can change it later in Settings"
                          : "",
                      style: isDesktop
                          ? STextStyles.desktopSubtitleH2(context)
                          : STextStyles.subtitle(context),
                    ),
                    SizedBox(
                      height: isDesktop ? 32 : 36,
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(
                        horizontal: isDesktop ? 0 : 16,
                      ),
                      child: PrivacyToggle(
                        externalCallsEnabled: isEasy,
                        onChanged: (externalCalls) {
                          isEasy = externalCalls;
                          setState(() {
                            infoToggle = isEasy;
                          });
                        },
                      ),
                    ),
                    SizedBox(
                      height: isDesktop ? 16 : 36,
                    ),
                    Padding(
                      padding: isDesktop
                          ? const EdgeInsets.all(0)
                          : const EdgeInsets.all(16.0),
                      child: RoundedWhiteContainer(
                        child: Center(
                          child: RichText(
                            textAlign: TextAlign.left,
                            text: TextSpan(
                              style: isDesktop
                                  ? STextStyles.desktopTextExtraExtraSmall(
                                      context,
                                    )
                                  : STextStyles.label(context).copyWith(
                                      fontSize: 12.0,
                                    ),
                              children: infoToggle
                                  ? [
                                      if (Constants.enableExchange)
                                        const TextSpan(
                                          text:
                                              "Exchange data preloaded for a seamless experience.\n\n",
                                        ),
                                      const TextSpan(
                                        text:
                                            "CoinGecko enabled: (24 hour price change shown in-app, total wallet value shown in USD or other currency).\n\n",
                                      ),
                                      TextSpan(
                                        text:
                                            "Recommended for most crypto users.",
                                        style: isDesktop
                                            ? STextStyles
                                                .desktopTextExtraExtraSmall600(
                                                context,
                                              )
                                            : TextStyle(
                                                color: Theme.of(context)
                                                    .extension<StackColors>()!
                                                    .textDark,
                                                fontWeight: FontWeight.w600,
                                              ),
                                      ),
                                    ]
                                  : [
                                      if (Constants.enableExchange)
                                        const TextSpan(
                                          text:
                                              "Exchange data not preloaded (slower experience).\n\n",
                                        ),
                                      const TextSpan(
                                        text:
                                            "CoinGecko disabled (price changes not shown, no wallet value shown in other currencies).\n\n",
                                      ),
                                      TextSpan(
                                        text:
                                            "Recommended for the privacy conscious.",
                                        style: isDesktop
                                            ? STextStyles
                                                .desktopTextExtraExtraSmall600(
                                                context,
                                              )
                                            : TextStyle(
                                                color: Theme.of(context)
                                                    .extension<StackColors>()!
                                                    .textDark,
                                                fontWeight: FontWeight.w600,
                                              ),
                                      ),
                                    ],
                            ),
                          ),
                        ),
                      ),
                    ),
                    if (!isDesktop)
                      const Spacer(
                        flex: 4,
                      ),
                    if (isDesktop)
                      const SizedBox(
                        height: 32,
                      ),
                    Padding(
                      padding: isDesktop
                          ? const EdgeInsets.all(0)
                          : const EdgeInsets.symmetric(
                              horizontal: 16,
                              vertical: 16,
                            ),
                      child: Row(
                        children: [
                          Expanded(
                            child: PrimaryButton(
                              label: !widget.isSettings
                                  ? "Continue"
                                  : "Save changes",
                              onPressed: () {
                                ref
                                    .read(prefsChangeNotifierProvider)
                                    .externalCalls = isEasy;

                                DB.instance
                                    .put<dynamic>(
                                  boxName: DB.boxNamePrefs,
                                  key: "externalCalls",
                                  value: isEasy,
                                )
                                    .then((_) {
                                  if (isEasy) {
                                    if (AppConfig.hasFeature(AppFeature.swap)) {
                                      unawaited(
                                        ExchangeDataLoadingService.instance
                                            .loadAll(),
                                      );
                                    }
                                    // unawaited(
                                    //     BuyDataLoadingService().loadAll(ref));
                                    ref
                                        .read(priceAnd24hChangeNotifierProvider)
                                        .start(true);
                                  }
                                });
                                if (!widget.isSettings) {
                                  if (isDesktop) {
                                    Navigator.of(context).pushNamed(
                                      CreatePasswordView.routeName,
                                    );
                                  } else {
                                    Navigator.of(context).pushNamed(
                                      CreatePinView.routeName,
                                    );
                                  }
                                } else {
                                  Navigator.pop(context);
                                }
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                    if (isDesktop)
                      const SizedBox(
                        height: kDesktopAppBarHeight,
                      ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class PrivacyToggle extends ConsumerStatefulWidget {
  const PrivacyToggle({
    super.key,
    required this.externalCallsEnabled,
    this.onChanged,
  });

  final bool externalCallsEnabled;
  final void Function(bool)? onChanged;

  @override
  ConsumerState<PrivacyToggle> createState() => _PrivacyToggleState();
}

class _PrivacyToggleState extends ConsumerState<PrivacyToggle> {
  late bool externalCallsEnabled;

  late final bool isDesktop;

  @override
  void initState() {
    isDesktop = Util.isDesktop;
    // initial toggle state
    externalCallsEnabled = widget.externalCallsEnabled;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final easyFile =
        ref.watch(themeProvider.select((value) => value.assets.personaEasy));
    final incognitoFile = ref
        .watch(themeProvider.select((value) => value.assets.personaIncognito));

    return Row(
      children: [
        Expanded(
          child: RawMaterialButton(
            elevation: 0,
            fillColor: Theme.of(context).extension<StackColors>()!.popupBG,
            shape: RoundedRectangleBorder(
              side: !externalCallsEnabled
                  ? BorderSide.none
                  : BorderSide(
                      color: Theme.of(context)
                          .extension<StackColors>()!
                          .infoItemIcons,
                      width: 2,
                    ),
              borderRadius: BorderRadius.circular(
                Constants.size.circularBorderRadius * 2,
              ),
            ),
            onPressed: () {
              setState(() {
                // update toggle state
                externalCallsEnabled = true;
              });
              // call callback with newly set value
              widget.onChanged?.call(externalCallsEnabled);
            },
            child: Padding(
              padding: const EdgeInsets.all(
                12,
              ),
              child: Stack(
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      (easyFile.endsWith(".png"))
                          ? Image.file(
                              File(
                                easyFile,
                              ),
                            )
                          : SvgPicture.file(
                              File(
                                easyFile,
                              ),
                              width: 140,
                              height: 140,
                            ),
                      Center(
                        child: Text(
                          "Easy Crypto",
                          style: isDesktop
                              ? STextStyles.desktopTextSmall(context)
                              : STextStyles.label700(context),
                        ),
                      ),
                      Center(
                        child: Text(
                          "Recommended",
                          style: isDesktop
                              ? STextStyles.desktopTextExtraExtraSmall(context)
                              : STextStyles.label(context),
                        ),
                      ),
                      if (isDesktop)
                        const SizedBox(
                          height: 12,
                        ),
                    ],
                  ),
                  if (externalCallsEnabled)
                    Positioned(
                      top: 4,
                      right: 4,
                      child: SvgPicture.asset(
                        Assets.svg.checkCircle,
                        width: 20,
                        height: 20,
                        color: Theme.of(context)
                            .extension<StackColors>()!
                            .infoItemIcons,
                      ),
                    ),
                  if (!externalCallsEnabled)
                    Positioned(
                      top: 4,
                      right: 4,
                      child: Container(
                        width: 20,
                        height: 20,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(1000),
                          color: Theme.of(context)
                              .extension<StackColors>()!
                              .textFieldDefaultBG,
                        ),
                      ),
                    ),
                ],
              ),
            ),
          ),
        ),
        const SizedBox(
          width: 16,
        ),
        Expanded(
          child: RawMaterialButton(
            elevation: 0,
            fillColor: Theme.of(context).extension<StackColors>()!.popupBG,
            shape: RoundedRectangleBorder(
              side: externalCallsEnabled
                  ? BorderSide.none
                  : BorderSide(
                      color: Theme.of(context)
                          .extension<StackColors>()!
                          .infoItemIcons,
                      width: 2,
                    ),
              borderRadius: BorderRadius.circular(
                Constants.size.circularBorderRadius * 2,
              ),
            ),
            onPressed: () {
              setState(() {
                // update toggle state
                externalCallsEnabled = false;
              });
              // call callback with newly set value
              widget.onChanged?.call(externalCallsEnabled);
            },
            child: Padding(
              padding: const EdgeInsets.all(
                12,
              ),
              child: Stack(
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      if (isDesktop)
                        const SizedBox(
                          height: 10,
                        ),
                      (incognitoFile.endsWith(".png"))
                          ? Image.file(
                              File(
                                incognitoFile,
                              ),
                            )
                          : SvgPicture.file(
                              File(
                                incognitoFile,
                              ),
                              width: 140,
                              height: 140,
                            ),
                      // SvgPicture.asset(
                      //   Assets.svg.personaIncognito(context),
                      //   width: 140,
                      //   height: 140,
                      // ),
                      if (isDesktop)
                        const SizedBox(
                          height: 12,
                        ),
                      Center(
                        child: Text(
                          "Incognito",
                          style: isDesktop
                              ? STextStyles.desktopTextSmall(context)
                              : STextStyles.label700(context),
                        ),
                      ),
                      Center(
                        child: Text(
                          "Privacy conscious",
                          style: isDesktop
                              ? STextStyles.desktopTextExtraExtraSmall(context)
                              : STextStyles.label(context),
                        ),
                      ),
                      if (isDesktop)
                        const SizedBox(
                          height: 12,
                        ),
                    ],
                  ),
                  if (!externalCallsEnabled)
                    Positioned(
                      top: 4,
                      right: 4,
                      child: SvgPicture.asset(
                        Assets.svg.checkCircle,
                        width: 20,
                        height: 20,
                        color: Theme.of(context)
                            .extension<StackColors>()!
                            .infoItemIcons,
                      ),
                    ),
                  if (externalCallsEnabled)
                    Positioned(
                      top: 4,
                      right: 4,
                      child: Container(
                        width: 20,
                        height: 20,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(1000),
                          color: Theme.of(context)
                              .extension<StackColors>()!
                              .textFieldDefaultBG,
                        ),
                      ),
                    ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }
}