import 'package:fcs/localization/transalation.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/bottom_widgets.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';

import '../../helpers/theme.dart';
import '../signin/signin_page.dart';
import '../widgets/banner.dart';
import '../widgets/offline_redirect.dart';

final msgLog = Logger('backgroundMessageHandler');

class WelcomePage extends StatefulWidget {
  @override
  _WelcomePageState createState() => _WelcomePageState();
}

typedef BtnCallback();

class _WelcomePageState extends State<WelcomePage> {
  final log = Logger('_HomePageWelcomeState');

  String pin;
  List<bool> isSelected = [true, false];

  @override
  void initState() {
    super.initState();
    _loadLang();
  }

  void _loadLang() {
    isSelected =
        Provider.of<LanguageModel>(context, listen: false).currentState;
  }

  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return OfflineRedirect(
      child: FlavorBanner(
        child: Scaffold(
            body: Container(
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment
                      .bottomCenter, // 10% of the width, so there are ten blinds.
                  colors: [
                    Color(0xd0272262),
                    Color(0xfa272262),
                  ], // whitish to gray
                )),
                child: Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        ToggleButtons(
                          children: <Widget>[
                            Image.asset(
                              'icons/flags/png/us.png',
                              package: 'country_icons',
                              fit: BoxFit.fitWidth,
                              width: 25,
                            ),
                            Image.asset(
                              'icons/flags/png/mm.png',
                              package: 'country_icons',
                              fit: BoxFit.fitWidth,
                              width: 25,
                            )
                          ],
                          selectedBorderColor: Colors.white12,
                          borderColor: Colors.transparent,
                          onPressed: _langChange,
                          isSelected: isSelected,
                        ),
                        FlatButton(
                          onPressed: () {
                            Navigator.of(context).push(CupertinoPageRoute(
                                builder: (context) => SigninPage()));
                          },
                          child: Text(
                            getLocalString(context, "welcome.signin"),
                            style: siginButtonStyle,
                          ),
                        ),
                      ],
                    ),
                    Expanded(
                      child: Column(
                        children: <Widget>[
                          Expanded(
                            child: Center(
                              child: Text(
                                getLocalString(context, "welcome.msg"),
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 18,
                                    fontFamily: "Roboto"),
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(top: 0),
                            child: CircleAvatar(
                                radius: (25),
                                backgroundColor: Colors.white,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(50),
                                  child: Image.asset("assets/logo.jpg"),
                                )),
                          ),
                          Padding(
                            padding:
                                const EdgeInsets.only(bottom: 10.0, top: 5),
                            child: Text(
                              "by FCS Logistics",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 11,
                                  fontFamily: "Roboto"),
                            ),
                          ),
                        ],
                      ),
                    ),
                    BottomWidgets(),
                  ],
                ))),
      ),
    );
  }

  _langChange(index) {
    var languageModel = Provider.of<LanguageModel>(context, listen: false);
    languageModel.saveLanguage(Translation().supportedLanguages[index]);
    setState(() {
      isSelected.asMap().forEach((i, e) {
        isSelected[i] = false;
      });
      isSelected[index] = !isSelected[index];
    });
  }
}
