import 'package:file_picker/file_picker.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:connectycube_sdk/connectycube_sdk.dart';
import 'package:vmeeting/service/routes/routes_name.dart';
import 'package:vmeeting/src/extension/context_extensions.dart';
import '../app_models/app_users_model/app_users_model.dart';
import '../src/constants/colors_const.dart';
import '../src/managers/push_notifications_manager.dart';
import '../src/utils/app_utils.dart';
import '../src/utils/pref_util.dart';
import '../src/widgets/image_avatar.dart';
import '../src/widgets/textfiled_widgets/auth_text_fild.dart';
import 'utils/api_utils.dart';
import 'utils/consts.dart';

class SettingsScreen extends StatelessWidget {
  final CubeUser currentUser;

  SettingsScreen(this.currentUser);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
         iconTheme: IconThemeData(color: ColorConst.appBleckColor),
          leading: IconButton(
            icon:  Icon(Icons.close, color: ColorConst.appBleckColor),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          automaticallyImplyLeading: false,
          title:  Text(
            'Settings',
            style: TextStyle(fontWeight: FontWeight.bold, color: ColorConst.appBleckColor),
          ),
          centerTitle: false,
        ),
        body: BodyLayout(currentUser),
        resizeToAvoidBottomInset: false);
  }
}

class BodyLayout extends StatefulWidget {
  final CubeUser currentUser;

  BodyLayout(this.currentUser);

  @override
  State<StatefulWidget> createState() {
    return _BodyLayoutState(currentUser);
  }
}

class _BodyLayoutState extends State<BodyLayout> {
  static const String TAG = "_BodyLayoutState";

  final CubeUser currentUser;
  var _isUsersContinues = false;
  String? _avatarUrl = "";
  final TextEditingController _loginFilter =  TextEditingController();
  final TextEditingController _nameFilter =  TextEditingController();
  final _nameFocusNode = FocusNode();
  final _loginFocusNode = FocusNode();
  final _emileFocusNode = FocusNode();
  final TextEditingController _emailFilter =  TextEditingController();
  String _login = "";
  String _name = "";
  String _email = "";

  _BodyLayoutState(this.currentUser) {
    _loginFilter.addListener(_loginListen);
    _nameFilter.addListener(_nameListen);
    _emailFilter.addListener(_emailListen);
    _nameFilter.text = currentUser.fullName ?? '';
    _loginFilter.text = currentUser.login ?? '';
    _emailFilter.text = currentUser.email ?? '';
  }

  void _loginListen() {
    if (_loginFilter.text.isEmpty) {
      _login = "";
    } else {
      _login = _loginFilter.text.trim();
    }
  }

  void _nameListen() {
    if (_nameFilter.text.isEmpty) {
      _name = "";
    } else {
      _name = _nameFilter.text.trim();
    }
  }

  void _emailListen() {
    if (_emailFilter.text.isEmpty) {
      _email = "";
    } else {
      _email = _emailFilter.text.trim();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Center(
          child: ConstrainedBox(
            constraints: const BoxConstraints(maxWidth: 400),
            child: Container(
              alignment: Alignment.center,
              padding: const EdgeInsets.all(8),
              child: Column(
                children: [
                  _buildAvatarFields(),
                  const SizedBox(height: 30),
                  _buildTextFields(),
                  const SizedBox(height: 50),
                  _buildButtons(),
                  Container(
                    margin: const EdgeInsets.only(left: 8),
                    child: Visibility(
                      maintainSize: false,
                      maintainAnimation: false,
                      maintainState: false,
                      visible: _isUsersContinues,
                      child: AppUtils.buttonLoader
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
  Widget buildImage(String? imagaAvatar) {
    return Center(
        child: AvatarImage(
          radius: context.h * 0.08,
          imagePath: getPrivateUrlForUid(imagaAvatar),
        ));
  }
  Widget _buildAvatarFields() {
    return Stack(
      children: <Widget>[
        InkWell(
          splashColor: greyColor2,
          borderRadius: BorderRadius.circular(45),
          onTap: () => _chooseUserImage(),
          child: buildImage(AppUtils.userModel.avatar ?? ""),
        ),
        Positioned(
          top: 75.0,
          right: 85.0,
          child: RawMaterialButton(
            onPressed: () {
              _chooseUserImage();
            },
            elevation: 2.0,
            fillColor: Colors.white,
            padding: const EdgeInsets.all(5.0),
            shape: const CircleBorder(),
            child: const Icon(
              Icons.mode_edit,
              size: 20.0,
            ),
          ),
        ),
      ],
    );
  }

  _chooseUserImage() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.image,
    );

    if (result == null) return;

    var uploadImageFuture = getUploadingImageFuture(result);

    uploadImageFuture.then((cubeFile) {
      _avatarUrl = cubeFile.getPublicUrl();
      setState(() {
        currentUser.avatar = _avatarUrl;
      });
    }).catchError((exception) {
      _processUpdateUserError(exception);
    });
  }

  Widget _buildTextFields() {
    return Column(
      children: <Widget>[
        AuthTextFild(
          lableName: "Change name",
          borderRadius: 10,
          elevation: 1,
          color: Colors.grey.withOpacity(0.2),
          type: TextInputType.emailAddress,
          focusNode: _nameFocusNode,
          textEditingController: _nameFilter,
        ),
        const SizedBox(height: 10),
        AuthTextFild(
          lableName: "Change login",
          borderRadius: 10,
          elevation: 1,
          color: Colors.grey.withOpacity(0.2),
          type: TextInputType.emailAddress,
          focusNode: _loginFocusNode,
          textEditingController: _loginFilter,
        ),
        const SizedBox(height: 10),
        AuthTextFild(
          lableName: "Change e-mail",
          borderRadius: 10,
          elevation: 1,
          color: Colors.grey.withOpacity(0.2),
          type: TextInputType.emailAddress,
          focusNode: _emileFocusNode,
          textEditingController: _emailFilter,
        ),
      ],
    );
  }

  Widget _buildButtons() {
    return Column(
      children: <Widget>[
        const SizedBox(
          height: 6,
        ),
        ElevatedButton(
          style: OutlinedButton.styleFrom(
            backgroundColor: ColorConst.appGreenColor,
            minimumSize: const Size(120, 36),
          ),
          onPressed: _updateUser,
          child: const Text('Save'),
        ),
        const SizedBox(
          height: 6,
        ),
        OutlinedButton.icon(
          style: OutlinedButton.styleFrom(
            foregroundColor: Colors.red.shade300,
            minimumSize: const Size(160, 36),
          ),
          icon: const Icon(
            Icons.delete,
            color: Colors.red,
          ),
          label: const Text(
            'Delete user',
            style: TextStyle(color: Colors.red),
          ),
          onPressed: _deleteUserPressed,
        ),
      ],
    );
  }

  void _updateUser() {
    if (_login.isEmpty && _name.isEmpty && _avatarUrl!.isEmpty && _email.isEmpty) {
      Fluttertoast.showToast(msg: 'Nothing to save');
      return;
    }
    var userToUpdate = CubeUser()..id = currentUser.id;
    if (_name.isNotEmpty) userToUpdate.fullName = _name;
    if (_login.isNotEmpty) userToUpdate.login = _login;
    if (_email.isNotEmpty) userToUpdate.email = _email;
    if (_avatarUrl!.isNotEmpty) userToUpdate.avatar = _avatarUrl;
    setState(() {
      _isUsersContinues = true;
    });
    updateUser(userToUpdate).then((user) {
      AppUtils.cubeUser = user;
      AppUtils.userModel = UserModel(
          login: user.login ?? "",
          sId: user.id.toString(),
          phone: user.phone ?? "",
          lastRequestAt: user.lastRequestAt?.toIso8601String() ?? "",
          isGuest: user.isGuest.toString(),
          facebookId: user.facebookId ?? "",
          externalUserId: user.externalUserId ?? "",
          externalId: user.externalId.toString(),
          customData: user.customData ?? "",
          createdAt: user.createdAt?.toIso8601String() ?? "",
          avatar: user.avatar ?? "",
          id: user.id,
          email: user.email ?? "",
          fullName: user.fullName ?? "",
          timezone: user.timeZone.toString() ?? "",
          twitterId: user.twitterId ?? "",
          updatedAt: user.updatedAt?.toIso8601String() ?? "",
          website: user.website.toString() ?? ""
      );
      Fluttertoast.showToast(msg: 'Success');
      setState(() {
        _isUsersContinues = false;
      });
    }).catchError((exception) {
      _processUpdateUserError(exception);
    });
  }

  void _logout() {
    print('_logout $_login and $_name');
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("Logout"),
          content: const Text("Are you sure you want logout current user"),
          actions: <Widget>[
            TextButton(
              child: const Text("CANCEL"),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            TextButton(
              child: const Text("OK"),
              onPressed: () {
                signOut().then(
                  (voidValue) {
                    Navigator.pop(context); // cancel current Dialog
                  },
                ).catchError(
                  (onError) {
                    Navigator.pop(context); // cancel current Dialog
                  },
                ).whenComplete(() {
                  CubeChatConnection.instance.destroy();
                  PushNotificationsManager.instance.unsubscribe();
                  FirebaseAuth.instance.currentUser?.unlink(PhoneAuthProvider.PROVIDER_ID);
                  SharedPrefs.deleteUserData();
                  Navigator.pop(context); // cancel current screen
                  _navigateToLoginScreen(context);
                });
              },
            ),
          ],
        );
      },
    );
  }

  void _deleteUserPressed() {
    _userDelete();
  }

  void _userDelete() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("Delete user"),
          content: const Text("Are you sure you want to delete current user?"),
          actions: <Widget>[
            TextButton(
              child: const Text("CANCEL"),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            TextButton(
              child: const Text("OK"),
              onPressed: () async {
                CubeChatConnection.instance.destroy();
                deleteUser(currentUser.id!).then(
                  (voidValue) {
                    Navigator.pushReplacementNamed(context, MainRoutes.sign_in_page);
                  },
                ).catchError(
                  (onError) {
                    Navigator.pop(context); // cancel current Dialog
                  },
                ).whenComplete(() async {
                  await PushNotificationsManager.instance.unsubscribe();
                  _navigateToLoginScreen(context);
                });
              },
            ),
          ],
        );
      },
    );
  }

  _navigateToLoginScreen(BuildContext context) {
    SharedPrefs.deleteUserData();
    Navigator.pushReplacementNamed(context, MainRoutes.sign_in_page);
  }

  void _processUpdateUserError(exception) {
    log("_processUpdateUserError error $exception", TAG);
    setState(() {
      _isUsersContinues = false;
    });
    showDialogError(exception, context);
  }
}
