import 'package:flutter/material.dart';
import 'package:screen_brightness/screen_brightness.dart';
import 'package:share_plus/share_plus.dart';

import '../../../../utils/clipboard_helper.dart';
import '../../../../widget/share_position_origin.dart';
import 'app_share_platform_view.dart';

const _androidUrl =
    'https://play.google.com/store/apps/details?id=eu.mhsl.marianum.mobile.client';
const _iosUrl = 'https://apps.apple.com/us/app/marianum-fulda/id6458789560';

class QrShareView extends StatefulWidget {
  const QrShareView({super.key});

  @override
  State<QrShareView> createState() => _QrShareViewState();
}

class _QrShareViewState extends State<QrShareView>
    with SingleTickerProviderStateMixin {
  // Own the TabController explicitly (not DefaultTabController) to dodge a
  // Flutter bug: TabBarView's didChangeDependencies fires jumpToPage mid-build,
  // whose scroll notification then calls setState on _TabStyle during the frame
  // — only reproduces with a bottomNavigationBar in the Scaffold underneath.
  late final TabController _tabController = TabController(
    length: 2,
    vsync: this,
  );

  @override
  void initState() {
    ScreenBrightness.instance.setApplicationScreenBrightness(1.0);
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    ScreenBrightness.instance.resetApplicationScreenBrightness();
    super.dispose();
  }

  void _shareLink(BuildContext context) {
    SharePlus.instance.share(
      ShareParams(
        sharePositionOrigin: SharePositionOrigin.get(context),
        subject: 'App Teilen',
        text:
            'Hol dir die für das Marianum maßgeschneiderte App:'
            '\n\nAndroid: $_androidUrl '
            '\niOS: $_iosUrl '
            '\n\nViel Spaß!',
      ),
    );
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: const Text('Teile die App'),
      bottom: TabBar(
        controller: _tabController,
        tabs: const [
          Tab(icon: Icon(Icons.android_outlined), text: 'Android'),
          Tab(icon: Icon(Icons.apple_outlined), text: 'iOS & iPadOS'),
        ],
      ),
    ),
    body: TabBarView(
      controller: _tabController,
      children: const [
        AppSharePlatformView('Für Android', _androidUrl),
        AppSharePlatformView('Für iOS & iPad', _iosUrl),
      ],
    ),
    bottomNavigationBar: SafeArea(
      child: Material(
        color: Theme.of(context).colorScheme.surface,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Divider(height: 1),
            Builder(
              builder: (innerCtx) => ListTile(
                leading: const Icon(Icons.share_outlined),
                title: const Text('Per Link teilen'),
                trailing: const Icon(Icons.arrow_right),
                onTap: () => _shareLink(innerCtx),
              ),
            ),
            ListTile(
              leading: const Icon(Icons.android_outlined),
              title: const Text('Android-Link kopieren'),
              trailing: const Icon(Icons.copy),
              onTap: () => copyToClipboard(context, _androidUrl),
            ),
            ListTile(
              leading: const Icon(Icons.apple_outlined),
              title: const Text('iOS-Link kopieren'),
              trailing: const Icon(Icons.copy),
              onTap: () => copyToClipboard(context, _iosUrl),
            ),
          ],
        ),
      ),
    ),
  );
}
