package com.unis.sxzcl.ui.announce;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.tencent.smtt.sdk.WebSettings;
import com.unis.model.AnnounceModel;
import com.unis.model.bean.AnnounceBean;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentAccounceDetailBinding;
import com.xyxsbj.mylibrary.tool.DateTool;
import com.xyxsbj.mylibrary.tool.HtmlUtil;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.greenrobot.eventbus.EventBus;


/**
 * 公告详情
 * 创建人: mQ
 * 时间: 2020/12/5 11:20
 */
public class AnnounceDetailFragment extends BaseFragment<FragmentAccounceDetailBinding> {

    private AnnounceBean mBean;

    @Override
    protected FragmentAccounceDetailBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentAccounceDetailBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        mBinding.bar.setTitle("公告")
                .setLeftClickListener(v -> onBack());

        //初始化webview
        WebSettings settings = mBinding.webView.getSettings();
        settings.setTextZoom(getWebTextZoom());
        //设置自适应屏幕，两者合用
        settings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
    }


    @Override
    protected void onFirstShow() {
        Bundle bundle = getArguments();
        String data = bundle.getString("data", null);
        if (data == null) {
            ToastTool.showLong("没有参数");
            onBack();
            return;
        }
        mBean = getGson().fromJson(data, AnnounceBean.class);
        mBinding.tvTitle.setText(mBean.getTitle());
        mBinding.tvSource.setText(mBean.getPublishOrgName());
        String publishTime = mBean.getPublishTime();
        try {
            publishTime = DateTool.formatDate(publishTime, DateTool.GLNZSJ_DATE, DateTool.YTDTD_DATE);
        } catch (Exception e) {
        }
        mBinding.tvTime.setText(publishTime);

        mBinding.webView.loadDataWithBaseURL(null, HtmlUtil.getHtmlData(mBean.getContent()), "text/html", "utf-8", null);

        if (!mBean.getReadStatus()) {
            LogTool.d(mBean.getId() + "置为已读");
            AnnounceModel.get().read(mBean.getId());
            EventBus.getDefault().post(mBean);
        }
    }


}
