package com.virjar.g4proxy;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.mm.R;
import com.virjar.g4proxy.service.SchedulerTaskService;
import com.virjar.g4proxy.utils.BDLocationUtils;
import com.virjar.g4proxy.utils.DeviceInfoHolder;
import com.virjar.g4proxy.utils.DeviceMessageUtil;
import com.virjar.g4proxy.utils.HttpClientUtils;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;
import top.wuhaojie.installerlibrary.AutoInstaller;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);

        Button button = findViewById(R.id.btn_report);
        button.setOnClickListener(this);

        String clientKey = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

        textView.setText("device: " + clientKey + "\n\n");

        BDLocationUtils bdLocationUtils = new BDLocationUtils(this);
        //开启定位
        bdLocationUtils.doLocation();
        //开始定位
        bdLocationUtils.mLocationClient.start();

        startService(new Intent(this, SchedulerTaskService.class));
        startService(new Intent(this, HttpProxyService.class));

        AutoInstaller.getDefault(this).autoConfigAccessibilitySetting(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        DeviceInfoHolder.initDeviceInfo(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        //不给权限的话，循环申请
        DeviceInfoHolder.initDeviceInfo(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_report) {
            //待上传的设备信息：devicesMsg
            JSONObject deviceMsg = DeviceMessageUtil.getDeviceMessage(this);

            String clientKey = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

            String text = "device: " + clientKey + "\n\n";
            textView.setText(text + deviceMsg);

            Request request = HttpClientUtils.postRequest("http://taskcenter.beta.qunar.com/api/phone/report", deviceMsg);

            HttpClientUtils.getClient().newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@javax.annotation.Nullable Call call, @javax.annotation.Nullable IOException e) {
                    Log.e(Constant.TAG, "report device msg failed", e);
                }

                @Override
                public void onResponse(@javax.annotation.Nullable Call call, @javax.annotation.Nullable Response response) {
                    try {
                        if (response != null) {
                            Log.e("report response", response.body().string());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
