package com.virjar.g4proxy;

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

import com.tencent.mm.R;
import com.virjar.g4proxy.download.DownLoadService;
import com.virjar.g4proxy.service.SchedulerTaskService;
import com.virjar.g4proxy.utils.BDLocationUtils;
import com.virjar.g4proxy.utils.DeviceInfoHolder;
import com.virjar.g4proxy.utils.DeviceMessageUtils;

import org.json.JSONObject;

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);


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

        startService(new Intent(this, SchedulerTaskService.class));
        startService(new Intent(this, HttpProxyService.class));
        startService(new Intent(this, DownLoadService.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 = DeviceMessageUtils.getDeviceMessage(this);

//            textView.setText(deviceMsg.toString());

            textView.setText(SchedulerTaskService.msgReportlUrl);
            textView.append("\r\n" + deviceMsg);

            SchedulerTaskService.runReportMessageTask(this);
        }
    }
}
