package com.police.policedatasystem.data.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.police.policedatasystem.databinding.ActivityKeyPersonDetailBinding;
import com.police.policedatasystem.model.KeyPersonDetail;
import com.police.policedatasystem.util.Constants;
import com.police.policedatasystem.util.UiUtils;

public class KeyPersonDetailActivity extends Activity {
    private ActivityKeyPersonDetailBinding binding;
    private KeyPersonDetail keyPersonDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityKeyPersonDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initListener();
        String sfzh = getIntent().getStringExtra("sfzh");
        keyPersonDetail = new Gson().fromJson(sfzh, KeyPersonDetail.class);
        binding.wm.setTextSize(50);
        binding.wm.setText(Constants.SFZH);
        showData();
    }

    private void showData() {
        /**
         * 基本信息数据显示
         */
        binding.tvName.setText(keyPersonDetail.getXm());
        binding.tvSex.setText(keyPersonDetail.getXb());
        if (UiUtils.isNotEmpty(keyPersonDetail.getHypo())) {
            binding.tvMarital.setVisibility(View.VISIBLE);
            binding.tvMarital.setText("已婚");
        }
        if (UiUtils.isNotEmpty(keyPersonDetail.getBq())) {
            binding.tvBq.setText(keyPersonDetail.getBq());
            binding.tvBq.setVisibility(View.VISIBLE);
        }
        setText(binding.tvNation, keyPersonDetail.getMz());
        setText(binding.detailSfzh, keyPersonDetail.getGmsfhm());
        if (UiUtils.isNotEmpty(keyPersonDetail.getJzdhhm())) {
            String[] split = keyPersonDetail.getJzdhhm().split(",");
            binding.detailSjh.setText(split[0]);//手机号
        }
        setText(binding.detailHj, keyPersonDetail.getJg());
        setText(binding.detailGldz, keyPersonDetail.getHjdz());
        setText(binding.detailFwcs, keyPersonDetail.getFwcs());
        if (UiUtils.isNotEmpty(keyPersonDetail.getDjchphm())) {
            binding.detailClxx.setText(keyPersonDetail.getDjchphm());//车辆信息
        }

        /**
         * 位置信息数据显示
         */

        if (UiUtils.isNotEmpty(keyPersonDetail.getSjgj())) {
            setText(binding.tvBodyLastLocationPhone, sCountSp(keyPersonDetail.getSjgj()));//手机基站位置信息
            setText(binding.tvBodyLastLocationPhoneTime, UiUtils.formatTime(keyPersonDetail.getSjgj().split(",")[1]));//手机最后定位时间
        }
        if (UiUtils.isNotEmpty(keyPersonDetail.getClzhwz())) {
            setText(binding.tvBodyLastLocation, sCountSp(keyPersonDetail.getClzhwz()));//车辆最后位置
            setText(binding.tvBodyLastTime, UiUtils.formatTime(keyPersonDetail.getClzhwz().split(",")[1]));//车辆最后定位时间
        }
        if (UiUtils.isNotEmpty(keyPersonDetail.getHcdp())) {
            setText(binding.tvBodyTrainInfo, keyPersonDetail.getHcdp().split(",")[3] + "，" + keyPersonDetail.getHcdp().split(",")[4] + " - " + keyPersonDetail.getHcdp().split(",")[5]);//火车订票
            setText(binding.tvBodyTrainTime, UiUtils.formatTime(keyPersonDetail.getHcdp().split(",")[2]));//火车发车时间
        }
        if (UiUtils.isNotEmpty(keyPersonDetail.getKcdp())) {
            setText(binding.tvBodyBusInfo, keyPersonDetail.getKcdp().split(",")[7] + "，" + keyPersonDetail.getKcdp().split(",")[2] + " - " + keyPersonDetail.getKcdp().split(",")[6]);//客车订票、、 tCount(keyPersonDetail.getKcdp())
            setText(binding.tvBodyBusTime, UiUtils.formatTime(keyPersonDetail.getKcdp().split(",")[4]));//客车发车时间
        }
        if (UiUtils.isNotEmpty(keyPersonDetail.getFjdp())) {
            setText(binding.tvBodyFlyInfo, keyPersonDetail.getFjdp().split(",")[4] + "，" + keyPersonDetail.getFjdp().split(",")[6] + " - " + keyPersonDetail.getFjdp().split(",")[9]);//飞机订票
            setText(binding.tvBodyFlyTime, UiUtils.formatTime(keyPersonDetail.getFjdp().split(",")[7]));//飞机起飞时间
        }
        if (UiUtils.isNotEmpty(keyPersonDetail.getZjzs())) {
            setText(binding.tvBodyRestInfo, tCount(keyPersonDetail.getZjzs()));//最近住宿
        }
        if (keyPersonDetail.getZp() != null) {
            loadBase64Image(binding.ivPhoto, keyPersonDetail.getZp());
        }

        /**
         * 列控数据信息显示
         */
        if (keyPersonDetail.getLsajcs() > 0) {
            setText(binding.tvBodyCtrlLevel, keyPersonDetail.getZdryLkxxList().get(0).getGklb());//管理级别
            setText(binding.tvBodyCtrlType, keyPersonDetail.getZdryLkxxList().get(0).getQtlb());//列管级别
            setText(binding.tvBodyCtrlTag, keyPersonDetail.getZdryLkxxList().get(0).getLkjz());//列控警种
            setText(binding.tvBodyCtrlTime, UiUtils.formatTime(keyPersonDetail.getZdryLkxxList().get(0).getLksj()));//列控时间
            setText(binding.tvBodyCtrlCause, keyPersonDetail.getZdryLkxxList().get(0).getLkyy());//列控原因
            setText(binding.tvBodyCtrlPerson, keyPersonDetail.getZdryLkxxList().get(0).getZrdw());//责任单位
            setText(binding.tvBodyCtrlPolice, keyPersonDetail.getZdryLkxxList().get(0).getZrmj());//责任民警
        }

    }

    public void loadBase64Image(ImageView imageView, String base64Str) {
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(base64Str)
                .into(imageView);
    }

    /**
     * 第3次索引
     *
     * @param str
     * @return
     */
    private String tCount(String str) {
        String c = ",";
        int firstIndex = str.indexOf(c);
        int secondIndex = str.indexOf(c, firstIndex + 1);
        int thirdIndex = str.indexOf(c, secondIndex + 1);
        return str.substring(thirdIndex + 1, str.length());
    }

    /**
     * 第2个索引
     *
     * @param str
     * @return
     */
    private String sCount(String str) {
        String c = ",";
        int firstIndex = str.indexOf(c);
        int secondIndex = str.indexOf(c, firstIndex + 1);
        return str.substring(secondIndex + 1, str.length());
    }

    /**
     * 第2个索引
     *
     * @param str
     * @return
     */
    private String sCountSp(String str) {
        try {
            return str.split(",")[3];
        } catch (Exception e) {
            return "";
        }
    }

    private void initListener() {
        binding.ivBack.setOnClickListener(view -> finish());
    }

    public static void startAction(Context context, String num) {
        Intent intent = new Intent(context, KeyPersonDetailActivity.class);
        intent.putExtra("sfzh", num);
        context.startActivity(intent);
    }

    private void setText(TextView view, String str) {
        if (UiUtils.isNotEmpty(str)) {
            view.setText(str);
        } else {
            view.setText("无");
        }
    }
}