import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:jubo/login.dart';
import 'package:jubo/model/person.dart';
import 'package:jubo/ui/changePassword.dart';
import 'package:jubo/ui/creditCards.dart';
import 'package:jubo/ui/loading.dart';
import 'package:jubo/ui/profile.dart';
import 'package:jubo/control/authService.dart';
import '../model/modelEvent.dart';
import 'package:intl/intl.dart';
import '../event_Artist.dart';
import '../util/params.dart';
import 'account.dart';
import 'formAccount.dart';

class Settings extends StatefulWidget {
  Settings();

  @override
  State createState() => _SettingsState();
}

class _SettingsState extends State<Settings> {
  bool loading = false;
  ModelPerson loggedPerson;

  void initState() {
    super.initState();
    getSettings();
  }

  Future<void> getSettings() async {
    setState(() {
      loading = true;
    });

    await Future.delayed(Duration(milliseconds: 500));

    AuthenticationService auth = new AuthenticationService();

    loggedPerson = await auth.getPerson();

    setState(() {
      loading = false;
    });
  }

  void _logout() async {
    setState(() => loading = true);
    await Future.delayed(Duration(seconds: 1));
    final auth = AuthenticationService();
    await auth.logout();
    Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(builder: (context) => Login()),
        (Route<dynamic> route) => false);
  }

  final List<Widget> aboutBoxChildren = <Widget>[
    const SizedBox(height: 24),
    RichText(
      text: TextSpan(
        children: <TextSpan>[
          TextSpan(
              style: TextStyle(color: COLOR_PRIMARY),
              text:
                  "JuBo é um acrônimo de Jukebox, sendo essa a essência do App, "
                  'bla bla bla bla.... visite nosso site '),
          TextSpan(
              style: TextStyle(color: Colors.blue), text: 'musicjubo.com.br'),
          TextSpan(
            text: '.',
            style: TextStyle(color: COLOR_PRIMARY),
          ),
        ],
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return loading
        ? Loading()
        : Container(
            color: COLOR_TERTIARY,
            child: ListView(
              padding: const EdgeInsets.all(15.0),
              children: [
                ListTile(
                  leading: Icon(
                    Icons.person,
                    color: Colors.white,
                  ),
                  title: Text(
                    "Meus dados",
                    style: TextStyle(
                      color: COLOR_FONT_1,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  onTap: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => Profile()));
                  },
                  trailing: Icon(
                    Icons.chevron_right,
                    color: Colors.white,
                  ),
                ),
                SizedBox(
                  height: 1.0,
                  child: Container(
                    color: COLOR_LINE_1,
                  ),
                ),
                loggedPerson.type == "Customer"
                    ? ListTile(
                        leading: Icon(Icons.credit_card, color: Colors.white),
                        title: Text(
                          "Meus Cartões",
                          style: TextStyle(
                            color: COLOR_FONT_1,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => CreditCards()));
                        },
                        trailing: Icon(
                          Icons.chevron_right,
                          color: Colors.white,
                        ),
                      )
                    : loggedPerson.type == "Artist"
                        ? ListTile(
                            leading: Icon(Icons.account_balance,
                                color: Colors.white),
                            title: Text(
                              "Minha Conta",
                              style: TextStyle(
                                color: COLOR_FONT_1,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                            onTap: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => Account()));
                            },
                            trailing: Icon(
                              Icons.chevron_right,
                              color: Colors.white,
                            ),
                          )
                        : SizedBox(height: 0),
                SizedBox(
                  height: 1.0,
                  child: Container(
                    color: COLOR_LINE_1,
                  ),
                ),
                ListTile(
                  leading: Icon(Icons.lock, color: Colors.white),
                  title: Text(
                    "Alterar minha senha",
                    style: TextStyle(
                      color: COLOR_FONT_1,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ChangePassword()));
                  },
                  trailing: Icon(
                    Icons.chevron_right,
                    color: Colors.white,
                  ),
                ),
                SizedBox(
                  height: 1.0,
                  child: Container(
                    color: COLOR_LINE_1,
                  ),
                ),
                ListTile(
                  leading: Icon(Icons.help, color: Colors.white),
                  title: Text(
                    'Sobre o App',
                    style: TextStyle(
                      color: COLOR_FONT_1,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  onTap: () {
                    showAboutDialog(
                        context: context,
                        applicationIcon: Image.asset(
                            'assets/images/Jubo_Splash.png',
                            width: 150),
                        applicationName: "Build",
                        applicationVersion: "1.0.0",
                        applicationLegalese: "\u{a9} 2021 JuBo software",
                        children: aboutBoxChildren);
                  },
                  trailing: Icon(
                    Icons.chevron_right,
                    color: Colors.white,
                  ),
                ),
                SizedBox(
                  height: 1.0,
                  child: Container(
                    color: COLOR_LINE_1,
                  ),
                ),
                ListTile(
                  leading: Icon(Icons.logout, color: Colors.white),
                  title: Text(
                    'Sair',
                    style: TextStyle(
                      color: COLOR_FONT_1,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  onTap: _logout,
                  trailing: Icon(
                    Icons.chevron_right,
                    color: Colors.white,
                  ),
                ),
                SizedBox(
                  height: 1.0,
                  child: Container(
                    color: COLOR_LINE_1,
                  ),
                ),
              ],
            ),
          );
  }
}
