import 'package:flutter/material.dart';
import 'package:memorias_calculo/src/back/scoped_models/user_info_model.dart';
import 'package:scoped_model/scoped_model.dart';

class ErrorPage extends StatefulWidget {
  @override
  _ErrorPageState createState() => _ErrorPageState();
}

class _ErrorPageState extends State<ErrorPage> {
  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<UserInfoModel>(
      builder: (context, child, model){
        return Scaffold(
          body: LayoutBuilder(
            builder: (context, constraints){
              final parentWidth = constraints.maxWidth;

              double containerWidth = parentWidth > 1440
                  ? parentWidth*0.5 : parentWidth > 1280 && parentWidth <= 1440
                  ? parentWidth*0.6 : parentWidth > 1080 && parentWidth <= 1280
                  ? parentWidth*0.75 : parentWidth > 900 && parentWidth <= 1080
                  ? parentWidth*0.8 : parentWidth > 720 && parentWidth <= 900
                  ? parentWidth*0.85 : parentWidth > 540 && parentWidth <= 720
                  ? parentWidth*0.9 : parentWidth;
              double containerMargin = (parentWidth - containerWidth) / 2;

              return SingleChildScrollView(
                child: Container(
                  width: containerWidth,
                  margin: EdgeInsets.symmetric(
                    horizontal: containerMargin,
                  ),
                  child: Column(
                    children: [
                      const SizedBox(height: 32.0),
                      Row(
                        children: [
                          const SizedBox(width: 32.0),
                          Image.asset(
                            "logo.png",
                            width: 64.0,
                          ),
                          Expanded(child: Container()),
                          Image.asset(
                            "logo_completo.png",
                            height: 64.0,
                          ),
                          const SizedBox(width: 32.0),
                        ],
                      ),
                      const SizedBox(
                        height: 32.0,
                      ),
                      const Icon(
                        Icons.cancel,
                        color: Color(0xFFFF0000),
                        size: 120.0,
                      ),
                      const SizedBox(
                        height: 48.0,
                      ),
                      Container(
                        width: double.infinity,
                        padding: const EdgeInsets.symmetric(
                          horizontal: 32.0,
                        ),
                        child: const Text(
                          "¡ERROR!",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontFamily: "Helvetica Neue Black Cond",
                            fontSize: 28.0,
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 8.0,
                      ),
                      Container(
                        padding: const EdgeInsets.symmetric(horizontal: 32.0),
                        width: double.infinity,
                        child: const Text(
                          "Por el momento tenemos incovenientes para conectarnos al servidor de S&P, algunas memorias de cálculo están disponibles sin conexión, "
                              "sin embargo no podrá modificar proyectos existentes y sus proyectos nuevos no se guardarán.",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontFamily: "Helvetica Neue Light",
                            fontSize: 22.0,
                            fontWeight: FontWeight.w600,
                            color: Color.fromRGBO(104, 104, 104, 1),
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 32.0,
                      ),
                      Container(
                        margin: const EdgeInsets.symmetric(
                          horizontal: 32.0,
                        ),
                        child: MaterialButton(
                          elevation: 4,
                          padding: const EdgeInsets.symmetric(vertical: 16.0),
                          minWidth: (containerWidth/2)-32,
                          color: const Color(0xFFFF0000),
                          disabledColor: Colors.grey,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Image.asset(
                                "continuar.png",
                                width: 32.0,
                              ),
                              const SizedBox(width: 8.0),
                              const Text(
                                "Continuar sin conexión",
                                style: TextStyle(
                                  fontFamily: "Helvetica Neue Black Cond",
                                  color: Colors.white,
                                  fontSize: 28.0,
                                ),
                              ),
                            ],
                          ),
                          onPressed: () async{
                            final continuar = await showDialog(
                              context: context,
                              barrierDismissible: false,
                              builder: (context) => AlertDialog(
                                title: const Text("Advertencia"),
                                content: const Text("Su ubicación será definida por defecto en México"),
                                actions: [
                                  TextButton(
                                    child: const Text("Aceptar"),
                                    onPressed: () => Navigator.pop(context, true),
                                  ),
                                  TextButton(
                                    child: const Text("Cancelar"),
                                    onPressed: () => Navigator.pop(context, false),
                                  ),
                                ],
                              ),
                            );

                            if(continuar){
                              model.updateNombreCompleto("Usuario");
                              model.updatePais("México (MX)");

                              Navigator.pushNamed(context, "index");
                            }
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 32.0,
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        );
      },
    );
  }
}

