import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/services/permission/permission_service.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/theme/theme_notifier.dart';
import 'package:diplomaticquarterapp/theme/theme_value.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:provider/provider.dart';
import 'package:screen_brightness/screen_brightness.dart';

class GeneralSettings extends StatefulWidget {
  @override
  _GeneralSettings createState() => _GeneralSettings();
}

class _GeneralSettings extends State<GeneralSettings> with TickerProviderStateMixin {
  var themeNotifier;
  int blindValue = 0;
  bool vibration = false;
  bool accsibility = false;
  bool camera = false;
  bool location = false;
  var sharedPref = new AppSharedPreferences();
  var permission = new PermissionService();
  LocationPermission locationPermission;
  var mediaQueryData;

  @override
  void initState() {
    this.getValues();
    super.initState();
  }

  Widget build(BuildContext context) {
    mediaQueryData = MediaQuery.of(context);
    themeNotifier = Provider.of<ThemeNotifier>(context);
    return Container(
      child: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(21.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                TranslationBase.of(context).modes,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  letterSpacing: -0.64,
                ),
              ),
              Row(
                children: [
                  Checkbox(
                    value: vibration,
                    onChanged: (value) {
                      setState(() {
                        vibration = value;
                      });
                    },
                    activeColor: CustomColors.accentColor,
                  ),
                  Text(
                    TranslationBase.of(context).vibration,
                    style: TextStyle(
                      fontSize: 12,
                      letterSpacing: -0.48,
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  Checkbox(
                    value: accsibility,
                    onChanged: (value) {
                      setState(() {
                        accsibility = value;
                      });
                    },
                    activeColor: CustomColors.accentColor,
                  ),
                  Text(
                    TranslationBase.of(context).accessibility,
                    style: TextStyle(
                      fontSize: 12,
                      letterSpacing: -0.48,
                    ),
                  ),
                ],
              ),
              mHeight(16),
              mHeight(12),
              Text(
                TranslationBase.of(context).blindMode,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  letterSpacing: -0.64,
                ),
              ),
              Column(
                children: [
                  Row(
                    children: [
                      new Radio(
                        value: 0,
                        groupValue: blindValue,
                        onChanged: (value) {
                          setState(() {
                            blindValue = value;
                            setTheme(value);
                          });
                        },
                      ),
                      Text(
                        TranslationBase.of(context).offTheme,
                        style: TextStyle(
                          fontSize: 12,
                          letterSpacing: -0.48,
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Radio(
                        value: 1,
                        groupValue: blindValue,
                        onChanged: (value) {
                          setState(() {
                            blindValue = value;
                            setTheme(value);
                          });
                        },
                      ),
                      Text(
                        TranslationBase.of(context).invertTheme,
                        style: TextStyle(
                          fontSize: 12,
                          letterSpacing: -0.48,
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Radio(
                        value: 2,
                        groupValue: blindValue,
                        onChanged: (value) {
                          setState(() {
                            blindValue = value;
                            setTheme(value);
                          });
                        },
                      ),
                      Text(
                        TranslationBase.of(context).dimTheme,
                        style: TextStyle(
                          fontSize: 12,
                          letterSpacing: -0.48,
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [
                      Radio(
                        value: 1,
                        groupValue: blindValue,
                        onChanged: (value) {
                          setState(() {
                            blindValue = value;
                            setTheme(value);
                          });
                        },
                      ),
                      Text(
                        TranslationBase.of(context).bwTheme,
                        style: TextStyle(
                          fontSize: 12,
                          letterSpacing: -0.48,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
              mHeight(12),
              mHeight(12),
              Text(
                TranslationBase.of(context).permissions,
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  letterSpacing: -0.64,
                ),
              ),
              Row(
                children: [
                  Checkbox(
                    value: camera,
                    onChanged: (value) {
                      setState(() {
                        camera = value;
                      });
                      this.permission.openSettings();
                    },
                    activeColor: CustomColors.accentColor,
                  ),
                  Text(
                    TranslationBase.of(context).cameraPermission,
                    style: TextStyle(
                      fontSize: 12,
                      letterSpacing: -0.48,
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  Checkbox(
                    value: location,
                    onChanged: (value) {
                      if (value == true) {
                        Navigator.pop(context);
                        this.setLocationPermission();
                      } else {
                        Geolocator.openLocationSettings();
                      }
                    },
                    activeColor: CustomColors.accentColor,
                  ),
                  Text(
                    TranslationBase.of(context).locationPermission,
                    style: TextStyle(
                      fontSize: 12,
                      letterSpacing: -0.48,
                    ),
                  ),
                ],
              ),
              mHeight(16),
            ],
          ),
        ),
      ),
    );
  }

  setLocationPermission() async {
    locationPermission = await Geolocator.checkPermission();
    if (locationPermission == LocationPermission.denied) {
      locationPermission = await Geolocator.requestPermission();
      if (locationPermission != LocationPermission.denied) {
        setState(() {
          location = true;
        });
      } else {
        this.location = false;
      }
    } else {
      Geolocator.openLocationSettings();
      this.location = true;
    }
  }

  setTheme(value) async {
    switch (value) {
      case 0:
        {
          themeNotifier.setTheme(await getTheme(value));
          setBrightness(1.0);
        }
        break;
      case 1:
        {
          themeNotifier.setTheme(await getTheme(value));
        }

        break;
      case 2:
        themeNotifier.setTheme(await getTheme(value));
        setBrightness(0.01);
        break;
      case 3:
        {
          themeNotifier.setTheme(await getTheme(value));
          setBrightness(1.0);
        }
        break;
      default:
        {
          themeNotifier.setTheme(await getTheme(value));
          setBrightness(1.0);
        }
        break;
    }
    permission.setTheme(value);
  }

  setBrightness(double brightness) async {
    try {
      await ScreenBrightness().setScreenBrightness(brightness);
    } catch (e) {
      print(e);
      throw 'Failed to set brightness';
    }
  }

  setVibration(value) {
    permission.setVibrationPermission(value);
  }

  setAccisibility() {
    if (!accsibility) {}
    // permission.setVibrationPermission(value);
  }

  getValues() async {
    if (permission.isThemeEnabled() != null) {
      blindValue = permission.isThemeEnabled() == null ? 0 : permission.isThemeEnabled();
      vibration = permission.isVibrationEnabled() == null ? false : permission.isVibrationEnabled();
      // accsibility = mediaQueryData.accessibleNavigation;
      //setTheme(blindValue);
    }
    var camera = await this.permission.isCameraEnabled();
    var location = await this.permission.isLocationEnabled();
    setState(() {
      this.camera = camera;
      this.location = location;
    });
  }

  getTheme(value) async {
    if (value == 1) {
      return invertThemes(fontName: Provider.of<ProjectViewModel>(context, listen: false).isArabic ? 'Cairo' : 'Poppins');
    } else if (value == 3) {
      return bwThemes(fontName: Provider.of<ProjectViewModel>(context, listen: false).isArabic ? 'Cairo' : 'Poppins');
    } else {
      return defaultTheme(fontName: Provider.of<ProjectViewModel>(context, listen: false).isArabic ? 'Cairo' : 'Poppins');
    }
  }
}
