// ignore_for_file: non_constant_identifier_names

import 'package:flutter/material.dart';
import 'package:general_package/enum_to_string/enum_to_string.dart';
import 'package:general_package/permisiion_converter/permission_converter.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
stringToAlignment(String? alignmentString){
  Alignment imageAlignment=Alignment.center;
  if (alignmentString=="bottomCenter") {
    imageAlignment=Alignment.bottomCenter;
  }else if (alignmentString=="bottomLeft") {
    imageAlignment=Alignment.bottomLeft;
  }else if (alignmentString=="bottomRight") {
    imageAlignment=Alignment.bottomRight;
  }else if (alignmentString=="centerLeft") {
    imageAlignment=Alignment.centerLeft;
  }else if (alignmentString=="centerRight") {
    imageAlignment=Alignment.centerRight;
  }else if (alignmentString=="topCenter") {
    imageAlignment=Alignment.topCenter;
  }else if (alignmentString=="topLeft") {
    imageAlignment=Alignment.topLeft;
  }else if (alignmentString=="topRight") {
    imageAlignment=Alignment.topRight;
  }
  return imageAlignment;
}
class OnboardModel {
  final int id;
  final String title;
  final String description;
  final String image;
  final Alignment imageAlignment;

  OnboardModel(
      {required this.id,
      required this.title,
      required this.description,
      required this.image,
      required this.imageAlignment,});

  static Map<String, dynamic> toMap(OnboardModel form) {
    var map = <String, dynamic>{};
    map['id'] = form.id;
    map['title'] = form.title;
    map['description'] = form.description;
    map['image'] = form.image;
    return map;
  }

  factory OnboardModel.fromJson(dynamic json) {
    return OnboardModel(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      image: json['image'],
      imageAlignment: stringToAlignment(json['imageAlignment']),
    );
  }
}

class permissionDescriptionModel {
  final int id;
  final String title;
  final String image;

  permissionDescriptionModel({
    required this.id,
    required this.title,
    required this.image,
  });
  static Map<String, dynamic> toMap(
      permissionDescriptionModel form) {
    var map = <String, dynamic>{};
    map['id'] =
        form.id;
    map['title'] =
        form.title;
    map['image'] =
        form.image;
    return map;
  }

  factory permissionDescriptionModel.fromJson(dynamic json) {
    return permissionDescriptionModel(
      id:
          json["id"],
      title:
          json['title'],
      image:
          json['image'],
    );
  }
}

enum requestTypes { location, storage, camera, contact }

class OnboardPermissionModel {
  final int id;
  final String title;
  final String image;
  final Permission request;
  final bool requestIsGranted;
  final List<permissionDescriptionModel>
      permissionDescription;

  OnboardPermissionModel(
      {required this.id,
      required this.title,
      required this.image,
      required this.request,
      required this.requestIsGranted,
      required this.permissionDescription});

  static Map<String, dynamic> toMap(OnboardPermissionModel form) {
    var map = <String, dynamic>{};
    map['id'] = form.id;
    map['title'] = form.title;
    map['image'] = form.image;
    map['request'] =
        EnumToString.convertToString(form.request);
    map['permissionDescription'] =
        form.permissionDescription.map(
            (permissionDescription) =>
                permissionDescriptionModel.toMap(
                    permissionDescription)).toList();
    return map;
  }

  factory OnboardPermissionModel.fromJson(dynamic json) {
    return OnboardPermissionModel(
      id: json["id"],
      title: json['title'],
      image: json['image'],
      request:
          PermissionConverter.fromString(json['request']),
      requestIsGranted: false,
      permissionDescription: json['permissionDescription']
          .map<permissionDescriptionModel>((permissionDescription) =>
              permissionDescriptionModel.fromJson(
                  permissionDescription))
          .toList(),
    );
  }
}

class OnboardMainModel {
  final String appIcon;
  final String splashImage;
  final List<OnboardModel> onboard;
  final List<OnboardPermissionModel> onboardPermission;

  OnboardMainModel(
      {required this.appIcon,
      required this.splashImage,
      required this.onboard,
      required this.onboardPermission});

  static Map<String, dynamic> toMap(OnboardMainModel form) {
    var map = <String, dynamic>{};
    map['appIcon'] = form.appIcon;
    map['splashImage'] = form.splashImage;
    map['onboard'] =
        form.onboard.map((Onboard) => OnboardModel.toMap(Onboard)).toList();
    map['onboardPermission'] = form.onboardPermission.map(
        (onboardPermission) =>
            OnboardPermissionModel.toMap(onboardPermission)).toList();
    return map;
  }

  factory OnboardMainModel.fromJson(dynamic json) {
    return OnboardMainModel(
      appIcon: json['appIcon'],
      splashImage: json['splashImage'],
      onboard: json['onboard']
          .map<OnboardModel>((Onboard) => OnboardModel.fromJson(Onboard))
          .toList(),
      onboardPermission: json['onboardPermission']
          .map<OnboardPermissionModel>((OnboardPermission) =>
              OnboardPermissionModel.fromJson(OnboardPermission))
          .toList(),
    );
  }
}

class OnboardController extends GetxController {
  final onboardMainModel = Rxn<OnboardMainModel>();
  set(data){
    onboardMainModel.value= OnboardMainModel.fromJson(data);
  }
  clear(){
    onboardMainModel.value= null;
  }
  @override
  void onInit() {
    ever(onboardMainModel, (value) => print("$value has been changed!!!"));
    super.onInit();
  }
}
final OnboardController onboardController = Get.put(OnboardController());