import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
import 'package:telnow_mobile_new/src/utils/U.dart';

import '../components/template.dart';

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

  @override
  State<EndSessionPage> createState() => _EndSessionPageState();
}

class _EndSessionPageState extends State<EndSessionPage> {
  @override
  Widget build(BuildContext context) {
    bool useAsset = false;
    String imageUrl = '';
    final ApiAuthProvider apiAuthProvider = ApiAuthProvider();

    try {
      imageUrl = apiAuthProvider.baseUrl + U.decodeBase64Url(Uri.decodeComponent(U.getAccessCode()!))+'/assets/background/tn';
      http.head(Uri.parse(imageUrl));
    }catch(e){
      useAsset = true;
    }

    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: [
          SizedBox(
              width: double.infinity, height: double.infinity,
              child: Image.network(imageUrl, fit: BoxFit.cover, width: double.infinity, height: double.infinity, errorBuilder: (context, error, stacktrace){
                return Image.asset('assets/image/background/background.jpg', fit: BoxFit.cover, width: double.infinity, height: double.infinity);
              })
          ),
          SingleChildScrollView(
            child: Container(
              padding: EdgeInsets.fromLTRB(16, kIsWeb?105:200, 16, 16),
              width: MediaQuery.of(context).size.width,
              height: kIsWeb && MediaQuery.of(context).size.height<700 ? 700 : MediaQuery.of(context).size.height,
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topCenter, end: Alignment.bottomCenter,
                      colors: [
                        primaryColor.withValues(alpha: 0.50), Colors.black.withValues(alpha: 0.91)
                      ]
                  )
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Image.asset('assets/image/logo/logo.png', width: 160),
                  Expanded(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Container(
                            margin: EdgeInsets.only(top: 100, left: 16, right: 16, bottom: 48),
                            child: Column(
                              children: [
                                Text('endSessionTitle'.tr(), style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500), textAlign: TextAlign.center),
                                SizedBox(height: 16),
                                Text('endSessionDesc'.tr(), style: TextStyle(color: Colors.white, fontSize: 16), textAlign: TextAlign.center),
                              ],
                            )
                        ),
                        GestureDetector(
                          onTap: (){
                            setState(() {
                              token.logout();
                              context.router.removeLast();
                              // context.replaceRoute(NamedRoute(name));
                              context.navigateToPath("/app/${U.getAccessCode()}");
                              // context.navigateToPath("/app/${U.getAccessCode()}/login");
                            });
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 32, horizontal: 64),
                            decoration: BoxDecoration(
                                color: primaryColor.withValues(alpha: 0.50),
                                border: Border.all(color: Colors.white),
                                borderRadius: BorderRadius.all(Radius.circular(12))
                            ),
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text('backToLogin'.tr(), style: TextStyle(color: Colors.white, fontSize: 16)),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 5),
                  Text('${'version'.tr()} ${ApiAuthProvider().displayVersion}', style: TextStyle(color: Colors.white, fontSize: 14)),
                  SizedBox(height: 5),
                  Text('Build ${ApiAuthProvider().buildNumber}', style: TextStyle(color: Colors.white, fontSize: 13)),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

// class EndSessionPage extends StatelessWidget {
//   const EndSessionPage({super.key});
//
//   @override
//   Widget build(BuildContext context) {
//
// }
