package com.joker.api;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.util.Log;

import com.joker.api.apply.ForceApplyPermissions;
import com.joker.api.apply.NormalApplyPermissions;
import com.joker.api.wrapper.AbstractWrapper;
import com.joker.api.wrapper.ActivityWrapper;
import com.joker.api.wrapper.FragmentWrapper;
import com.joker.api.wrapper.SupportFragmentWrapper;
import com.joker.api.wrapper.Wrapper;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static com.joker.api.Permissions4M.PageType.ANDROID_SETTING_PAGE;
import static com.joker.api.Permissions4M.PageType.MANAGER_PAGE;

/**
 * Created by joker on 2017/8/5.
 */

public class Permissions4M {
    public static Wrapper get(Activity activity) {
        return new ActivityWrapper(activity);
    }

    public static Wrapper get(android.app.Fragment fragment) {
        return new FragmentWrapper(fragment);
    }

    public static Wrapper get(android.support.v4.app.Fragment fragment) {
        return new SupportFragmentWrapper(fragment);
    }

    @SuppressWarnings("unchecked")
    public static void onRequestPermissionsResult(Activity activity, int
            requestCode, @NonNull int[] grantResults) {
        onPrivateRequestPermissionsResult(activity, requestCode, grantResults);
    }

    @SuppressWarnings("unchecked")
    public static void onRequestPermissionsResult(android.app.Fragment fragment, int
            requestCode, @NonNull int[] grantResults) {
        onPrivateRequestPermissionsResult(fragment, requestCode, grantResults);
    }

    @SuppressWarnings("unchecked")
    public static void onRequestPermissionsResult(android.support.v4.app.Fragment fragment, int
            requestCode, @NonNull int[] grantResults) {
        onPrivateRequestPermissionsResult(fragment, requestCode, grantResults);
    }

    private static void onPrivateRequestPermissionsResult(Object object, int
            requestCode, @NonNull int[] grantResults) {
        AbstractWrapper.Key key = new AbstractWrapper.Key(object, requestCode);
        Wrapper wrapper = AbstractWrapper.getWrapperMap().get(key);
        // because SupportFragment request permissions will call Activity callback first and then call SupportFragment callback
        // and the first time will throw NullPointerException
        if (wrapper == null) {
            return;
        }
        Wrapper.PermissionRequestListener requestListener = wrapper
                .getPermissionRequestListener();
        // listener callback
        if (requestListener != null) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (wrapper.isRequestForce()) {
                    ForceApplyPermissions.grantedOnResultWithListener(wrapper);
                } else {
                    NormalApplyPermissions.grantedWithListener(wrapper);
                }
            } else {
                NormalApplyPermissions.deniedOnResultWithListener(wrapper);
            }
        } else {
            // annotation callback
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (wrapper.isRequestForce()) {
                    Log.e("TAG", "onPrivateRequestPermissionsResult: 1 ");
                    ForceApplyPermissions.grantedOnResultWithAnnotation(wrapper);
                } else {
                    Log.e("TAG", "onPrivateRequestPermissionsResult: 2 ");
                    NormalApplyPermissions.grantedWithAnnotation(wrapper);
                }
            } else {
                Log.e("TAG", "onPrivateRequestPermissionsResult: 3 ");
                NormalApplyPermissions.deniedWithAnnotation(wrapper);
            }
        }
    }

    public static void requestPermission(Activity activity, String permission, int requestCode) {
        new ActivityWrapper(activity)
                .requestForce(true)
                .requestPermission(permission)
                .requestCode(requestCode)
                .request();
    }

    public static void requestPermission(android.app.Fragment fragment, String
            permission, int requestCode) {
        new FragmentWrapper(fragment)
                .requestForce(true)
                .requestPermission(permission)
                .requestCode(requestCode)
                .request();
    }

    public static void requestPermission(android.support.v4.app.Fragment fragment, String permission, int
            requestCode) {
        new SupportFragmentWrapper(fragment)
                .requestForce(true)
                .requestPermission(permission)
                .requestCode(requestCode)
                .request();
    }

    @IntDef({MANAGER_PAGE, ANDROID_SETTING_PAGE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface PageType {
        int MANAGER_PAGE = 1;
        int ANDROID_SETTING_PAGE = 0;
    }
}

@@ -3341,83 +3341,8 @@
 ) %7B%0A
-                    Log.e(%22TAG%22, %22onPrivateRequestPermissionsResult: 1 %22);%0A
     
@@ -3448,83 +3448,8 @@
 e %7B%0A
-                    Log.e(%22TAG%22, %22onPrivateRequestPermissionsResult: 2 %22);%0A
     
@@ -3562,79 +3562,8 @@
 e %7B%0A
-                Log.e(%22TAG%22, %22onPrivateRequestPermissionsResult: 3 %22);%0A
     

delete log code