import 'package:auto_route/annotations.dart';
import 'package:drift/src/runtime/api/runtime_api.dart';
import 'package:drift_db_viewer/drift_db_viewer.dart';
import 'package:flutter/material.dart';
import 'package:get_it_mixin/get_it_mixin.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:remever/common/functions.dart';
import 'package:remever/common/widgets/typography.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/database/database.dart';
import 'package:remever/widgets/primary_button.dart';
import 'package:talker_flutter/talker_flutter.dart';

import '../../components/notifiers/app_settings.dart';
import '../../components/env.dart';
import '../../inject.dart';

@RoutePage()
class SandboxScreen extends StatefulWidget {
  const SandboxScreen({super.key});

  @override
  State<SandboxScreen> createState() => _SandboxScreenState();
}

class _SandboxScreenState extends State<SandboxScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const AppTypography('Песочница')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: <Widget>[
          _buildVersion(),
          const HSpace(8),
          Text('${getIt<Env>().runtimeType}:${getIt<Env>().url}'),
          const HSpace(8),
          _debugBox(),
          const HSpace(8),
          PrimaryButton(
            child: const Text('Логи'),
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute<dynamic>(
                  builder:
                      (BuildContext context) => TalkerScreen(talker: talker),
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _debugBox() {
    return DecoratedBox(
      decoration: const BoxDecoration(
        border: Border.fromBorderSide(
          BorderSide(width: 1, color: Colors.black),
        ),
        borderRadius: BorderRadius.all(Radius.circular(4)),
      ),
      child: Column(
        children: <Widget>[
          const AppTypography('debug'),
          ShowFpsSetting(),
          EnableDebugSetting(),
          ElevatedButton(
            onPressed: () {
              Navigator.of(context).push(
                MaterialPageRoute<dynamic>(
                  builder: (BuildContext context) {
                    return DriftDbViewer(getIt<AppDatabase>());
                  },
                ),
              );
            },
            child: const Text('Open Db Viewer'),
          ),
        ],
      ),
    );
  }

  Widget _buildVersion() {
    return FutureBuilder<PackageInfo>(
      future: PackageInfo.fromPlatform(),
      builder: (BuildContext context, AsyncSnapshot<PackageInfo> snapshot) {
        return AppTypography(
          'Версия: ${snapshot.data?.version ?? '-'}'
          '+${snapshot.data?.buildNumber ?? '-'}',
          type: Bold14px(),
          //color: Colors.black,
        );
      },
    );
  }
}

class ShowFpsSetting extends StatelessWidget with GetItMixin {
  ///
  /// Виджет для контроля показа FPS
  ///
  ShowFpsSetting({super.key});

  @override
  Widget build(BuildContext context) {
    final bool showFPS = watchOnly<AppSettingsNotifier, bool>(
      (AppSettingsNotifier e) => e.showFps,
    );

    return SwitchListTile.adaptive(
      value: showFPS,
      title: const Text('Show FPS'),
      activeColor: Colors.blueAccent,
      onChanged: (_) {
        if (context.mounted) get<AppSettingsNotifier>().toggleFps();
      },
    );
  }
}

class EnableDebugSetting extends StatelessWidget with GetItMixin {
  ///
  /// Виджет для включения общего дебага
  ///
  EnableDebugSetting({super.key});

  @override
  Widget build(BuildContext context) {
    final bool debugMode = watchOnly<AppSettingsNotifier, bool>(
      (AppSettingsNotifier e) => e.debugMode,
    );

    return SwitchListTile.adaptive(
      value: debugMode,
      title: const Text('Debug mode'),
      activeColor: Colors.blueAccent,
      onChanged: (_) => get<AppSettingsNotifier>().toggleDebugMode(),
    );
  }
}
