package com.yutou.jianr_mg.plugins.Activitys.Utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.widget.Toast;


import com.yutou.jianr_mg.plugins.Activitys.MainActivity;

import org.json.JSONObject;

import com.yutou.jianr_mg.plugins.Interfaces.PluginHttpInterface;

public class PlugsAdTools {
    public static int req=0,show=1,click=2;
    public static int Open=1,Inter=2,Banner=3,Native=4;
    private static Context context;
    public static void upload(final Context context, int adType, int uType){
        try {
            PlugsAdTools.context=context;
            JSONObject json=new JSONObject();
            json.put("adtype",adType);
            json.put("utype",uType);
            json.put("imei",getIMEI());
            PluginHttpTools.init().httpGet( MainActivity.homeurl+"android/ad/user.do?adtype="+adType+"&utype="+uType+"&imei="+getIMEI(), new PluginHttpInterface() {
                @Override
                public void httpGetData(Object string, int state) {
                    try{
                        JSONObject json=new JSONObject(string.toString());
                        if(json.getInt("code")==100){
                            JSONObject data=json.getJSONObject("data");
                            final String msg=data.getString("msg");
                            System.out.println("服务器回调："+msg);
                            if(data.getInt("code")==100){
                                MainActivity.handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
                                    }
                                });
                            }
                            /*if(!data.isNull("user")){
                                AppData.userdata= JSON.parseObject(data.getJSONObject("user").getJSONObject("data").getJSONObject("userdata").toString(), UUserdata.class);
                            }*/
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void httpError(Exception e) {

                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @SuppressLint("MissingPermission")
    private static String getIMEI() {
        String imei="-1";
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            imei = telephonyManager.getDeviceId();
        }
        return imei;
    }
}
