package com.qy.agv.activity;

import static com.qy.agv.util.Constant.CACHE_BIND_CONTROL;
import static com.qy.agv.util.Constant.CACHE_LOGIN_KEY;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.provider.Settings;
import android.view.View;

import com.qy.agv.activity.model.EventMessage;
import com.qy.agv.comm.BaseActivity;
import com.qy.agv.comm.CacheService;
import com.qy.agv.databinding.QyRobotInfoBinding;
import com.qy.agv.update.UpdateManager;
import com.qy.agv.update.UpdateXmlService;
import com.qy.agv.util.Constant;
import com.qy.agv.util.ProjectUtils;
import com.qy.agv.util.StringUtils;
import com.qy.agv.util.ToastUtils;
import com.qy.agv.util.VersionUtils;

import org.apache.log4j.Logger;
import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.HashMap;


public class RobotInfoActivity extends BaseActivity {
    public static Logger mylog =Logger.getLogger(RobotInfoActivity.class);

    private Context context;
    private QyRobotInfoBinding binding;
    private HashMap<String,String> mHashMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = QyRobotInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        x.view().inject(this);
        context = this;
        initCommonTop2();
        init();
    }

    @SuppressLint("ClickableViewAccessibility")
    public void init(){
        if(Constant.user!=null) {
            binding.tvLoginName.setText(Constant.user.getReal_name());
        }else{
            binding.tvLoginName.setText("未登录");
        }
        binding.tvWorkStation.setText("操作员");
        binding.tvVersion.setText(VersionUtils.getVerName(context));
        binding.tvDeviceSeq.setText(getMac());
        binding.versionUpdate.setOnClickListener(view->{
            updateApp();
        });
        binding.exit.setOnClickListener(view->{
            myLogout();
        });
    }

    private void updateApp(){
        try {
            boolean haveInstallPermission = context.getPackageManager().canRequestPackageInstalls();
            Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
            if (!haveInstallPermission) {
                context.startActivity(intent);
            }
        }catch (NoSuchMethodError e){
            e.printStackTrace();
        }
        if(isUpdate()) {
            ProjectUtils.init();
            UpdateManager updateManager = new UpdateManager(context, mHashMap);
            updateManager.showDownloadDialog();
        }else {
            ToastUtils.showToast(this, "已经是最新版本了");
        }
    }

    public boolean isUpdate(){
        try {
            int versionCode = VersionUtils.getVerCode(this);
            UpdateXmlService service = new UpdateXmlService();
            String path = ProjectUtils.getProjectURL() + "/version.xml";
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(5 * 1000);
            conn.setConnectTimeout(10000);
            conn.setRequestMethod("GET");
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
            InputStream inStream = conn.getInputStream();
            mHashMap = service.parseXml(inStream);
            String current_version = mHashMap.get("version");
            if (StringUtils.isEmpty(current_version)) {
                return false;
            }
            if (null != mHashMap) {
                int serviceCode = Integer.parseInt(current_version);
                return serviceCode > versionCode;
            }
        } catch (SocketTimeoutException ex){
            mylog.error("连接超过:", ex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void receiverTask(String barcode) {

    }

    @Override
    protected String getCommonTopTitle() {
        return "设备信息";
    }

    @Override
    protected int getCommonTopICO() {
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
