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

@RoutePage()
class ErrorPage extends StatefulWidget {
  final code;
  final errMsg;

  ErrorPage(this.code, this.errMsg);

  @override
  _ErrorPageState createState() => _ErrorPageState();
}

class _ErrorPageState extends State<ErrorPage> {
  final ApiAuthProvider _apiAuthProvider = ApiAuthProvider();
  String stringError = '';
  String imageUrl = '';

  @override
  void initState() {
    try{imageUrl = _apiAuthProvider.baseUrl + U.decodeBase64Url(Uri.decodeComponent(U.getAccessCode()!))+'/assets/background/tn';}catch(e){}
    // TODO: implement initState
    switch (widget.errMsg) {
      case "_strErrVer":
        stringError = "invalidVersion".tr() +
          ". " +
          "sugestVersion".tr() + " " +
          U.getVersion().toString() +
          ". " +
          "currentVersion".tr() + " " +
          ApiAuthProvider().currentVersion.toString() + ".";
        break;
        default:
          stringError = widget.errMsg;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            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);
            }),
          ),
          Container(
            // decoration: BoxDecoration(color: Color(0xff0D497F).withValues(alpha: 0.85)),
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter, end: Alignment.bottomCenter,
                    colors: [
                      primaryColor.withValues(alpha: 0.50), Colors.black.withValues(alpha: 0.91)
                    ]
                )
            ),
          ),
          Center(child: Padding( padding: EdgeInsets.all(24.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: EdgeInsets.only(bottom: 30),
                  child: Image(
                    image: new AssetImage("assets/image/logo/logo.png"),
                    width: 100,
                  ),
                ),
                widget.code != '' ? Text(
                  widget.code,
                  style: TextStyle(color: Colors.white, fontSize: 86),
                ) : Container(),
                Text(
                  stringError,
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: 18),
                ),
              ],
            ),
          ))
        ],
      ),
    );
  }
}
