package com.kfzs.duanduan.oem;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.datashare.DDProviderHelper;
import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.util.DeviceUtil;
import com.sheep.gamegroup.util.NetUtil;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;


import org.greenrobot.eventbus.EventBus;


/**
 * Created by Administrator on 2017/12/22.
 */

public class AppAddOrdelReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
            try {

                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String dataString = intent.getDataString();
                        if(TextUtils.isEmpty(dataString)){
                            return;
                        }
                        String packages = dataString.replace("package:","");
                        final DownLoadInfo downLoadInfo = DDProviderHelper.getInstance()
                                .getDownloadTaskToPackname(context, packages);
                        if(downLoadInfo == null){
                            return;
                        }
                        new Thread(){
                            @Override
                            public void run() {
                                super.run();
                                try {
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("device_id",DeviceUtil.getDeviceId(SheepApp.mContext)+"");
                                    jsonObject.put("id",downLoadInfo.getMGameID());
                                    NetUtil.getNetUtil().sendAppDownload(context, jsonObject);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }.start();
                    }
                }, 0);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

        if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
            ApkUtils.getInstance().clear();
            EventBus.getDefault().post(intent);
        }

        /*
          安装失败
         */
        if(Intent.ACTION_INSTALL_FAILURE.equals(intent.getAction())){
            try {

                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        String dataString = intent.getDataString();
                        if(TextUtils.isEmpty(dataString)){
                            return;
                        }
                        String packages = dataString.replace("package:","");
                        if(packages.equals(SheepApp.getInstance().getPackgeName()+"")){
                            G.showToast("安装失败，请检查是否空间不足或其他问题!");
                        }
                    }
                }, 0);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

}
