import { NativeModules} from 'react-native';
import ToastLibrary from './Toast'

function packageParams(params:any[]):any[]{
    let packagedParams=[];
    for(let i=0;i<params.length;i++){
      if(typeof params[i]=='number'){
        // 数字封装带类型注释的对象
        if(params[i].toString().match(/^[0-9]+$/)!=null){
          // 整数
          packagedParams.push(params[i]+":"+"int");
        }else{
          // double浮点数
          packagedParams.push(params[i]+":"+"double");
        }
      }else{
        // 其它或者字符串按原样
        packagedParams.push(params[i]);
      }
    }

    return packagedParams;
}

// 该方法按原样调用，其它方法再进行其它处理
export function useJavaNative(className:string,methodName:string,params:any[],packageName:string="",useObj:string=""):Promise<string>{
  return new Promise((resolve)=>{
    // 调用方法
    NativeModules.UseNative.useJavaNative(className,methodName,packageParams(params),packageName,useObj).then((ret:any)=>{
      resolve(ret);
    })
  }
  );
}

// 使用Native中的静态方法
export async function useJavaStatic(className:string,methodName:string,params:any[],packageName:string=""):Promise<string>{
  let ret=await useJavaNative(className,methodName,params,packageName,"");
  return ret;
}

export async function useJava(objectName:string,methodName:string,params:any[]):Promise<string>{
  let ret=await useJavaNative("",methodName,params,"",objectName);
  return ret;
}

// 创建Native中的对象
export async function createJavaObject(className:String,params:any[],packageName:string=""):Promise<string>{
  return new Promise((resolve)=>{
    // 调用方法
    NativeModules.UseNative.createJavaObject(className,packageParams(params),packageName).then((ret:string)=>{
      resolve(ret);
    })
  })
}

// 获得Native基本类型或其包装类的值
export async function getJavaObjectValue(objectName:string):Promise<number|string>{
  return new Promise((resolve)=>{
    NativeModules.UseNative.getJavaObjectValue(objectName).then((ret:number|string)=>{
      resolve(ret);
    })
  })
}

export const $app="$app"; // 指代当前上下文
export const $appnative="$appnative"; 
export async function getJavaCurrentWindow():Promise<string>{
  let resp=await NativeModules.UseNative.getJavaCurrentActivity();
  return resp;
}

export const Toast=ToastLibrary;

// 待测试导出代码
export async function getJavaObjectArrayElement(objectName:string,idx:number,genericType:string=""):Promise<string>{
  return new Promise((resolve)=>{
    NativeModules.UseNative.getJavaObjectArrayElement(objectName,idx,genericType).then((resp:string)=>{
      resolve(resp);
    })
  })
}

export function poolLength():Promise<number>{
  return new Promise(resolve=>{
    NativeModules.UseNative.poolLength().then((resp:number)=>resolve(resp));
  })
}

export function getJavaObjectArrayLength(objectName:string):Promise<number>{
  return new Promise(resolve=>{
    NativeModules.UseNative.getJavaObjectArrayLength(objectName).then((resp:number)=>resolve(resp))
  })
}

export function convertTo(objectName:string,newClassName:string):Promise<string>{
  return new Promise(resolve=>{
    NativeModules.UseNative.castObject(objectName,newClassName).then((resp:string)=>resolve(resp))
  })
}

export function getJavaObjectType(objectName:string):Promise<string>{
  return new Promise(resolve=>{
    NativeModules.UseNative.getJavaObjectType(objectName).then((resp:string)=>resolve(resp));
  })
}

export function createJavaArrayNative(elementJavaType:string,length:number,params:any[]):Promise<string>{
  return new Promise(resolve=>{
    NativeModules.UseNative.createJavaArray(elementJavaType,length,params).then((resp:string)=>resolve(resp))
  })
}

export function createJavaArray(elementJavaType:string,params:any[]):Promise<string>{
  return new Promise(resolve=>{
    NativeModules.UseNative.createJavaArray(elementJavaType,params.length,params).then((resp:string)=>resolve(resp))
  })
}