package xn.xxp.home.notice;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import http.client.HttpTool;
import http.vo.response.LaboratoryVo;
import xn.xxp.R;
import xn.xxp.databinding.ActivityNoticeMsgCollegeBinding;
import xn.xxp.databinding.ItemNoticeCollegeBinding;
import xn.xxp.app.LabApp;

public class NoticeMsgCollegeActivity extends AppCompatActivity {
    ActivityNoticeMsgCollegeBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoticeMsgCollegeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.nowDate.setText(TimeUtils.getNowString(new SimpleDateFormat("MM月dd日 EEEE ", Locale.getDefault())));
        binding.backLayout.setOnClickListener(v -> finish());
        LaboratoryVo laboratoryVo = LabApp.laboratory;
        if (null != laboratoryVo) {
            Glide.with(this).load(laboratoryVo.rectangleLogo).into(binding.logo);
            binding.subName.setText(laboratoryVo.subName);
        }
        Intent getIntent = getIntent();
        if (null != getIntent) {
            if (getIntent.hasExtra("json")) {
                String json = getIntent.getStringExtra("json");
                RectifyMsg.RectifyMsgBean data = GsonUtils.fromJson(json, RectifyMsg.RectifyMsgBean.class);

                // 检查机构
                String checkDeptName = data.getCheckDeptName();
                checkDeptName = TextUtils.isEmpty(checkDeptName) ? "" : checkDeptName;
                // 实验室名称房间号
                String subName = data.getSubName();
                subName = TextUtils.isEmpty(subName) ? "" : subName;
                //检查年
                String checkYear = data.getCheckYear();
                checkYear = TextUtils.isEmpty(checkYear) ? "" : checkYear;
                // 检查月
                String checkMonth = data.getCheckMonth();
                checkMonth = TextUtils.isEmpty(checkMonth) ? "" : checkMonth;
                // 检查日
                String checkDay = data.getCheckDay();
                checkDay = TextUtils.isEmpty(checkDay) ? "" : checkDay;
                // 计划标题
                String planName = data.getPlanName();
                planName = TextUtils.isEmpty(planName) ? "" : planName;
                // 整改隐患数量
                int count = data.getCount();
                //  整改期限年
                String deadlineYear = data.getDeadlineYear();
                deadlineYear = TextUtils.isEmpty(deadlineYear) ? "" : deadlineYear;
                // 整改期限月
                String deadlineMonth = data.getDeadlineMonth();
                deadlineMonth = TextUtils.isEmpty(deadlineMonth) ? "" : deadlineMonth;
                // 整改期限日
                String deadlineDay = data.getDeadlineDay();
                deadlineDay = TextUtils.isEmpty(deadlineDay) ? "" : deadlineDay;
                //  检查机构电子签章
                String checkDeptSign = data.getCheckDeptSign();
                checkDeptSign = TextUtils.isEmpty(checkDeptSign) ? "" : checkDeptSign;
                //  房间号
                String roomNum = data.getRoomNum();
                roomNum = TextUtils.isEmpty(roomNum) ? "" : roomNum;
                // 检查完整时间
                String checkDate = data.getCheckDate();
                checkDate = TextUtils.isEmpty(checkDate) ? "" : checkDate;


                binding.viewSafeNotice.titleCheckDeptNameTV.setText(checkDeptName);
                binding.viewSafeNotice.titleSubNameTV.setText(subName);
                // 内容
                String content = "               学院于" + checkYear + "年" + checkMonth + "月" + checkDay
                        + "日对你实验室实施了" + planName + "，共发现了" + count + "个整改项（见附件）。请逐一对照、分析原因、采取有效措施实施整改，" +
                        "并于" + deadlineYear + "年" + deadlineMonth + "月" + deadlineDay + "日在系统里提交整改证明材料和整改报告";
                binding.viewSafeNotice.noticeRectifyContentTV.setText(content);

                // 印章
                String signUrl = HttpTool.checkUrl(checkDeptSign);
                Glide.with(NoticeMsgCollegeActivity.this)
                        .load(signUrl)
                        .placeholder(R.mipmap.test_seal)
                        .error(R.mipmap.test_seal)
                        .into(binding.viewSafeNotice.checkDeptSignIV);
                binding.viewSafeNotice.signCheckDeptNameTV.setText(checkDeptName);
                // 检查时间
                binding.viewSafeNotice.signCheckNyrTV.setText(checkYear + "年" + checkMonth + "月" + checkDay + "日");

                // 汇总标题
                binding.viewSafeNotice.summaryTitleCheckDeptNameTV.setText(checkDeptName);
                // 汇总被查实验室
                binding.viewSafeNotice.summarySubNameTV.setText(subName);
                // 汇总被查房号
                binding.viewSafeNotice.summaryRoomNumTV.setText(roomNum);
                // 汇总检查日期
                String summaryN = "年";
                String summaryY = "月";
                String summaryR = "日";
                if (!TextUtils.isEmpty(checkDate)) {
                    String[] checkDateArray = checkDate.split("-", -1);
                    if (checkDate.length() > 2) {
                        summaryN = checkDateArray[0];
                        summaryY = checkDateArray[0];
                        summaryR = checkDateArray[0];
                    }
                }
                binding.viewSafeNotice.summaryCheckDateTV.setText(summaryN + "年" + summaryY + "月" + summaryR + "日");
                // 明细
                List<RectifyMsg.RectifyMsgBean.DetailListBean> dataDetailList = data.getDetailList();
                if (null != dataDetailList && !dataDetailList.isEmpty()) {
                    ItemNoticeCollegeBinding itemNoticeCollegeBinding;
                    for (int i = 0; i < dataDetailList.size(); i++) {
                        itemNoticeCollegeBinding = ItemNoticeCollegeBinding.inflate(getLayoutInflater());
                        RectifyMsg.RectifyMsgBean.DetailListBean detailListBean = dataDetailList.get(i);
                        // 序号
                        String index = detailListBean.getIndex();
                        index = TextUtils.isEmpty(index) ? "0" : index;
                        itemNoticeCollegeBinding.noTV.setText(index);
                        // 条款号
                        String optionCode = detailListBean.getOptionCode();
                        optionCode = TextUtils.isEmpty(optionCode) ? "0" : optionCode;
                        itemNoticeCollegeBinding.termNoTV.setText(optionCode);
                        // 存在问题描述
                        String checkDesc = detailListBean.getCheckDesc();
                        checkDesc = TextUtils.isEmpty(checkDesc) ? "0" : checkDesc;
                        itemNoticeCollegeBinding.desTV.setText(checkDesc);
                        // 6张照片加载
                        ImageView imageView1 = loadImage(detailListBean.getCheckImg1());
                        if (null != imageView1)
                            itemNoticeCollegeBinding.imgBoxlayout.addView(imageView1);
                        ImageView imageView2 = loadImage(detailListBean.getCheckImg2());
                        if (null != imageView2)
                            itemNoticeCollegeBinding.imgBoxlayout.addView(imageView2);
                        ImageView imageView3 = loadImage(detailListBean.getCheckImg3());
                        if (null != imageView3)
                            itemNoticeCollegeBinding.imgBoxlayout.addView(imageView3);
                        ImageView imageView4 = loadImage(detailListBean.getCheckImg4());
                        if (null != imageView4)
                            itemNoticeCollegeBinding.imgBoxlayout.addView(imageView4);
                        ImageView imageView5 = loadImage(detailListBean.getCheckImg5());
                        if (null != imageView5)
                            itemNoticeCollegeBinding.imgBoxlayout.addView(imageView5);
                        ImageView imageView6 = loadImage(detailListBean.getCheckImg6());
                        if (null != imageView6)
                            itemNoticeCollegeBinding.imgBoxlayout.addView(imageView6);
                        binding.viewSafeNotice.desLL.addView(itemNoticeCollegeBinding.getRoot());
                    }
                }
            }
        }
    }

    private ImageView loadImage(String url) {
        String http;
        if (TextUtils.isEmpty(url)) {
            return null;
        } else {
            http = HttpTool.checkUrl(url);
        }
        LogUtils.d("加载图片", http);
        ImageView imageView = new ImageView(this);
        ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(320, 240);
        layoutParams.setMargins(10, 10, 10, 10);
        imageView.setLayoutParams(layoutParams);
        imageView.setImageResource(R.mipmap.icon_qd_loading);
        imageView.setOnClickListener(v -> {
            ImageView clickImageView = (ImageView) v;
            openBigPic(clickImageView.getDrawable());
        });
        Glide.with(this).load(http).error(R.mipmap.img_error).into(imageView);
        return imageView;
    }

    private void openBigPic(Drawable drawable) {
        binding.bigLL.setVisibility(View.VISIBLE);
        binding.bigIV.setImageDrawable(drawable);
        binding.backBigIV.setOnClickListener(v -> closeBigPic());
    }

    private void closeBigPic() {
        binding.bigLL.setVisibility(View.GONE);
    }
}