import 'dart:convert';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:crypto/crypto.dart';

class Constants {
  var title;
  var message;
  var color;
  late AlertDialog alert;

  Constants(String title, String message) {
    this.title = title;
    this.message = message;
  }

  Constants1(String color) {
    this.color = color;
  }

  static const TextStyle fontStyle = TextStyle(
      fontFamily: 'Poppins', fontSize: 18, fontWeight: FontWeight.normal);

  static const TextStyle headerfontStyle = TextStyle(
      fontFamily: 'Poppins', fontSize: 16, fontWeight: FontWeight.w600);

  static const TextStyle listHeaderfontStyle = TextStyle(
      fontFamily: 'Poppins', fontSize: 16, fontWeight: FontWeight.w600);

  static const TextStyle listHeaderoppfontStyle = TextStyle(
      fontFamily: 'Poppins', fontSize: 14, fontWeight: FontWeight.normal);

  static const TextStyle listHeadercolorfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 16,
      fontWeight: FontWeight.w400,
      color: Colors.red);

  static const TextStyle listHeadercolor1fontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 16,
      fontWeight: FontWeight.w400,
      color: Colors.green);

  static const TextStyle headerlistitems = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.normal,
      height: 1.2);

  static const TextStyle headerlistitems1 = TextStyle(
      fontFamily: 'Poppins', fontSize: 14, fontWeight: FontWeight.bold);

  static const TextStyle open = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.bold,
      color: Colors.red);

  static const TextStyle close = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.bold,
      color: Colors.green);

  static const TextStyle listitems = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 12,
      fontWeight: FontWeight.normal,
      color: Colors.black);

  static const TextStyle listheaders = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: Color.fromARGB(255, 106, 36, 36));

  static const TextStyle buttonsfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 16,
      fontWeight: FontWeight.w600,
      color: Colors.white);

  static const TextStyle whitefontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.w400,
      color: Colors.white);

  static const TextStyle footfallheaderfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 22,
      fontWeight: FontWeight.bold,
      color: Colors.white);

  static const TextStyle footfallfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 12,
      fontWeight: FontWeight.normal,
      color: Colors.white);

  static const TextStyle reviewbuttonsfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: Colors.black);

  static const TextStyle registerfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: Colors.green);

  static const TextStyle unregisterfontStyle = TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: Colors.red);

  static Future<void> showAlertDialog1(BuildContext context) async {
    AlertDialog alert = const AlertDialog(
      content: Row(
        children: [
          CircularProgressIndicator(),
        ],
      ),
    );
    await showDialog(
        context: context, builder: (BuildContext context) => alert);
  }

  static showLoader(BuildContext ctx) {
    showDialog(
        context: ctx,
        builder: (context) {
          return const Center(child: CircularProgressIndicator());
        });
  }

  static Future<void> dismissAlertDialog1(BuildContext context) async {
    Navigator.pop(context);
  }

  static Future<bool> checkInternetConnectivity() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.none) {
      return false;
    } else {
      return true;
    }
  }

  static LinearGradient getReturn() {
    const LinearGradient gradient = LinearGradient(
      colors: [
        // Color(0xFF8c4756),
        Color(0xFF503b53),
        Color(0xFF503b53),
        //Color(0xFF7b32fe)
      ],
      begin: Alignment.bottomLeft,
      end: Alignment.bottomRight,
    );
    return gradient;
  }

  static Future<void> saveLoginCredentials(
      String username,
      String empid,
      String storeslist,
      String storeName,
      String accessLevel,
      bool validate) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString('userId', username);
    prefs.setString('empId', empid);
    prefs.setString('stores_list', storeslist);
    prefs.setString('access', accessLevel);
    //prefs.setString('storeName', storeName);
    prefs.setBool('islogged', validate);
  }

  Future<bool?> getLoggedIn() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    bool? validate = preferences.getBool('islogged');
    return validate;
  }

  Future<String?> getStoreId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString('store_Id');
  }

  static String hashPassword(String password) {
    var bytes = utf8.encode(password);
    var digest = sha256.convert(bytes);
    return digest.toString();
  }

  static String capitalizeFirstLetter(String text) {
    if (text.isEmpty) {
      return text;
    }
    return text[0].toUpperCase() + text.substring(1);
  }

  static dialogMessage(BuildContext context, String title, String message) {
    Widget okButton = TextButton(
        child: const Text("Cancel"),
        onPressed: () {
          Navigator.of(context).pop();
        });
    AlertDialog alert = AlertDialog(
      title: Text(title),
      content: Text(message),
      actions: [
        okButton,
      ],
    );
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

  static double getDropdownItemHeight(BuildContext context) {
    double deviceHeight = MediaQuery.of(context).size.height;
    double dropdownItemHeight = deviceHeight / 1.6;
    return dropdownItemHeight;
  }

  showAlertDialog(BuildContext context, bool val) {
    Widget okButton = TextButton(
        child: const Text("Cancel"),
        onPressed: () {
          if (val) {
            Navigator.popUntil(context, (route) => route.isFirst);
          } else {
            Navigator.of(context).pop();
          }
        });
    AlertDialog alert = AlertDialog(
      title: Text(title),
      content: Text(message),
      actions: [okButton],
    );
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }
}
