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.ActivityNoticeMsgBinding;
import xn.xxp.databinding.IncludeNoticeSchoolDesBinding;
import xn.xxp.databinding.ItemNoticeSchoolBinding;
import xn.xxp.app.LabApp;

/**
 * 校级整改通知
 */
public class NoticeMsgSchoolActivity extends AppCompatActivity {
    private ActivityNoticeMsgBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoticeMsgBinding.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 rectifyMsgBean = GsonUtils.fromJson(json, RectifyMsg.RectifyMsgBean.class);
                String deptName = rectifyMsgBean.getDeptName();
                deptName = TextUtils.isEmpty(deptName) ? "" : deptName;
                String planName = rectifyMsgBean.getPlanName();
                planName = TextUtils.isEmpty(planName) ? "" : planName;
                String checkYear = rectifyMsgBean.getCheckYear();
                checkYear = TextUtils.isEmpty(checkYear) ? "" : checkYear;
                String checkMonth = rectifyMsgBean.getCheckMonth();
                checkMonth = TextUtils.isEmpty(checkMonth) ? "" : checkMonth;
                String checkDay = rectifyMsgBean.getCheckDay();
                checkDay = TextUtils.isEmpty(checkDay) ? "" : checkDay;
                String deadlineYear = rectifyMsgBean.getDeadlineYear();
                deadlineYear = TextUtils.isEmpty(deadlineYear) ? "" : deadlineYear;
                String deadlineMonth = rectifyMsgBean.getDeadlineMonth();
                deadlineMonth = TextUtils.isEmpty(deadlineMonth) ? "" : deadlineMonth;
                String deadlineDay = rectifyMsgBean.getDeadlineDay();
                deadlineDay = TextUtils.isEmpty(deadlineDay) ? "" : deadlineDay;
                String code = rectifyMsgBean.getCode();
                code = TextUtils.isEmpty(code) ? "" : code;
                int count = rectifyMsgBean.getCount();
                String checkDeptName = rectifyMsgBean.getCheckDeptName();
                checkDeptName = TextUtils.isEmpty(checkDeptName) ? "" : checkDeptName;
                String checkDate = rectifyMsgBean.getCheckDate();
                checkDate = TextUtils.isEmpty(checkDate) ? "" : checkDate;

                // 通知标题学校
                binding.viewSafeNotice.noticeTitleDeptNameTV.setText(deptName);
                // 通知标号
                binding.viewSafeNotice.noticeTitleNoTV.setText(code);
                // 通知收件人
                binding.viewSafeNotice.noticeDeptNameTV.setText(deptName);
                // 通知内容
                String contentSB = "               学校于" +
                        (TextUtils.isEmpty(checkYear) ? "年" : checkYear) + "年" +
                        (TextUtils.isEmpty(checkMonth) ? "月" : checkMonth) + "月" +
                        (TextUtils.isEmpty(checkDay) ? "日" : checkDay) + "日" +
                        "开展了" + (TextUtils.isEmpty(planName) ? "" : planName) + "，共发现了" +
                        count + "个整改项（见附件）。请逐一对照、分析原因、采取有效措施实施整改，并于" +
                        (TextUtils.isEmpty(deadlineYear) ? "年" : deadlineYear) + "年" +
                        (TextUtils.isEmpty(deadlineMonth) ? "月" : deadlineMonth) + "月" +
                        (TextUtils.isEmpty(deadlineDay) ? "日" : deadlineDay) + "日" +
                        "在系统里提交整改证明材料和整改报告。";
                binding.viewSafeNotice.noticeRectifyContentTV.setText(contentSB);
                // 通知发件人
                binding.viewSafeNotice.noticeCheckDeptNameTV.setText(checkDeptName);
                // 通知日期
                binding.viewSafeNotice.noticeCheckNyrTV.setText(checkYear + "年" + checkMonth + "月" + checkDay + "日");

                /*汇总*/
                // 汇总标题学校
                binding.viewSafeNotice.summaryTitleDeptNameTV.setText(deptName);
                // 汇总被检查单位
                binding.viewSafeNotice.summaryDeptNameTV.setText(deptName);
                // 汇总检查日期
                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.TitleBean> titleBeanList = rectifyMsgBean.getTitleList();
                if (!titleBeanList.isEmpty()) {

                    ItemNoticeSchoolBinding itemNoticeSchoolBinding;
                    IncludeNoticeSchoolDesBinding includeNoticeDesBinding;
                    for (int i = 0; i < titleBeanList.size(); i++) {
                        itemNoticeSchoolBinding = ItemNoticeSchoolBinding.inflate(getLayoutInflater());
                        RectifyMsg.RectifyMsgBean.TitleBean titleBean = titleBeanList.get(i);
                        // 序号
                        String index = titleBean.getIndex();
                        index = TextUtils.isEmpty(index) ? "0" : index;
                        itemNoticeSchoolBinding.noTV.setText(index);
                        // 条款号
                        String optionCode = titleBean.getOptionCode();
                        optionCode = TextUtils.isEmpty(optionCode) ? "0" : optionCode;
                        itemNoticeSchoolBinding.termNoTV.setText(optionCode);

                        // 明细详情
                        List<RectifyMsg.RectifyMsgBean.TitleBean.DetailBean> detailList = titleBean.getDetailList();
                        if (!detailList.isEmpty()) {
                            for (int j = 0; j < detailList.size(); j++) {
                                includeNoticeDesBinding = IncludeNoticeSchoolDesBinding.inflate(getLayoutInflater());
                                RectifyMsg.RectifyMsgBean.TitleBean.DetailBean detailBean = detailList.get(0);
                                // 实验室名字
                                String subName = detailBean.getSubName();
                                subName = TextUtils.isEmpty(subName) ? "实验室" : subName;
                                includeNoticeDesBinding.itemSubNameTV.setText(subName);
                                // 问题描述
                                String checkDesc = detailBean.getCheckDesc();
                                checkDesc = TextUtils.isEmpty(checkDesc) ? "" : checkDesc;
                                includeNoticeDesBinding.itemCheckDesTV.setText(checkDesc);
                                // 5张照片加载
                                ImageView imageView1 = loadImage(detailBean.getCheckImg1());
                                if (null != imageView1)
                                    includeNoticeDesBinding.imgBoxlayout.addView(imageView1);
                                ImageView imageView2 = loadImage(detailBean.getCheckImg2());
                                if (null != imageView2)
                                    includeNoticeDesBinding.imgBoxlayout.addView(imageView2);
                                ImageView imageView3 = loadImage(detailBean.getCheckImg3());
                                if (null != imageView3)
                                    includeNoticeDesBinding.imgBoxlayout.addView(imageView3);
                                ImageView imageView4 = loadImage(detailBean.getCheckImg4());
                                if (null != imageView4)
                                    includeNoticeDesBinding.imgBoxlayout.addView(imageView4);
                                ImageView imageView5 = loadImage(detailBean.getCheckImg5());
                                if (null != imageView5)
                                    includeNoticeDesBinding.imgBoxlayout.addView(imageView5);
                                ImageView imageView6 = loadImage(detailBean.getCheckImg6());
                                if (null != imageView6)
                                    includeNoticeDesBinding.imgBoxlayout.addView(imageView6);
                                itemNoticeSchoolBinding.desLL.addView(includeNoticeDesBinding.getRoot());
                            }
                        }
                        binding.viewSafeNotice.desLL.addView(itemNoticeSchoolBinding.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).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);
    }
}