package com.joker.api.wrapper;

import android.annotation.SuppressLint;

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

abstract class FragmentBaseWrapper extends AbstractWrapper implements Wrapper {
    public FragmentBaseWrapper() {

    }

    @SuppressWarnings("unchecked")
    @SuppressLint("NewApi")
    void tryRequestWithAnnotation() {
        Object context = getContext();
        int requestCode = getRequestCode();
        String requestPermission = getRequestPermission();
        if (context instanceof android.support.v4.app.Fragment) {
            if (((android.support.v4.app.Fragment) context).shouldShowRequestPermissionRationale
                    (requestPermission)) {
                if (!getProxy(context.getClass().getName()).customRationale(context, requestCode)) {
                    getProxy(context.getClass().getName()).rationale(context, requestCode);
                    originalRequest();
                }
            } else {
                originalRequest();
            }
        } else {
            if (((android.app.Fragment) context).shouldShowRequestPermissionRationale
                    (requestPermission)) {
                if (!getProxy(context.getClass().getName()).customRationale(context,
                        requestCode)) {
                    getProxy(context.getClass().getName()).rationale(context, requestCode);
                    originalRequest();
                }
            } else {
                originalRequest();
            }
        }
    }

    @SuppressLint("NewApi")
    void tryRequestWithListener() {
        PermissionCustomRationaleListener customRationaleListener = getPermissionCustomRationaleListener();
        Object context = getContext();
        int requestCode = getRequestCode();
        String requestPermission = getRequestPermission();
        PermissionRequestListener requestListener = getPermissionRequestListener();

        if (context instanceof android.app.Fragment) {
            if (((android.app.Fragment) context).shouldShowRequestPermissionRationale
                    (requestPermission)) {
                if (customRationaleListener != null) {
                    customRationaleListener.permissionCustomRationale(getRequestCode());
                } else {
                    requestListener.permissionRationale(requestCode);
                    originalRequest();
                }
            } else {
                originalRequest();
            }
        } else {
            if (((android.support.v4.app.Fragment) context).shouldShowRequestPermissionRationale
                    (requestPermission)) {
                if (customRationaleListener != null) {
                    customRationaleListener.permissionCustomRationale(getRequestCode());
                } else {
                    requestListener.permissionRationale(requestCode);
                    originalRequest();
                }
            } else {
                originalRequest();
            }
        }
    }

    @Override
    @SuppressLint("NewApi")
    void originalRequest() {
        Object context = getContext();
        int requestCode = getRequestCode();
        String requestPermission = getRequestPermission();
        if (context instanceof android.app.Fragment) {
            ((android.app.Fragment) context).requestPermissions(new String[]{requestPermission},
                    requestCode);
        } else {
            ((android.support.v4.app.Fragment) context).requestPermissions(new
                    String[]{requestPermission}, requestCode);
        }
    }
}

@@ -3111,39 +3111,41 @@
 ();%0A        
-int requestCode
+String permission
  = getReques
@@ -3137,36 +3137,42 @@
 ion = getRequest
-Code
+Permission
 ();%0A        Stri
@@ -3171,32 +3171,16 @@
     
-String requestPermission
+int code
  = g
@@ -3184,34 +3184,28 @@
 = getRequest
-Permission
+Code
 ();%0A        
@@ -3324,32 +3324,25 @@
 ew String%5B%5D%7B
-requestP
+p
 ermission%7D,%0A
@@ -3344,37 +3344,10 @@
 on%7D,
-%0A                    requestC
+ c
 ode)
@@ -3473,24 +3473,17 @@
 tring%5B%5D%7B
-requestP
+p
 ermissio
@@ -3486,24 +3486,17 @@
 ssion%7D, 
-requestC
+c
 ode);%0A  

refactor code