package com.rdiot.yx485.message;

import static android.view.View.GONE;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.rdiot.yx485.BaseActivity;
import com.rdiot.yx485.databinding.MeasstypeBinding;

public class MessageListActivity extends BaseActivity {
    private MeasstypeBinding mBinding;
    private MessageViewModel mViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = MeasstypeBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        initView();
        initData();
    }

    private void initData() {
        mViewModel = new ViewModelProvider(this).get(MessageViewModel.class);
        mViewModel.getErrorMessage().observe(this, this::showMessage);
        mViewModel.getMessageCount().observe(this, list -> {
            if (list == null) {
                return;
            }

            mBinding.sysnumber.setVisibility(list.system.unread_count > 0 ? View.VISIBLE : GONE);
            mBinding.sysnumber.setText(String.valueOf(list.system.unread_count));
            mBinding.systime.setVisibility(list.system.total > 0 ? View.VISIBLE : GONE);
            mBinding.systime.setText(list.system.last_time);
            mBinding.tvSystemContent.setText(getMessageStr(list.system.content));

            mBinding.devicenumber.setVisibility(list.device.unread_count > 0 ? View.VISIBLE : GONE);
            mBinding.devicenumber.setText(String.valueOf(list.device.unread_count));
            mBinding.devicetime.setVisibility(list.device.total > 0 ? View.VISIBLE : GONE);
            mBinding.devicetime.setText(list.device.last_time);
            mBinding.tvDeviceContent.setText(getMessageStr(list.device.content));

            mBinding.actionnumber.setVisibility(list.activity.unread_count > 0 ? View.VISIBLE : GONE);
            mBinding.actionnumber.setText(String.valueOf(list.activity.unread_count));
            mBinding.actiontime.setVisibility(list.activity.total > 0 ? View.VISIBLE : GONE);
            mBinding.actiontime.setText(list.activity.last_time);
            mBinding.tvActiveContent.setText(getMessageStr(list.activity.content));
        });
    }

    private void initView() {
        mBinding.layoutTitle.tvTitle.setText("消息");
        mBinding.layoutTitle.btnTitleLeft.setOnClickListener(view -> finish());
        mBinding.syslayout.setOnClickListener(view -> {
            //消息类型 1系统消息（天气、系统通知） 2设备消息（设备故障、设备耗材、场景执行） 3活动消息
            Intent intent = new Intent();
            intent.putExtra("type", "1");
            intent.putExtra("name", "系统消息");
            intent.setClass(MessageListActivity.this, MessageContentListActivity.class);
            startActivity(intent);
        });
        mBinding.devicelayout.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("type", "2");
            intent.putExtra("name", "设备消息");
            intent.setClass(MessageListActivity.this, MessageContentListActivity.class);
            startActivity(intent);
        });
        mBinding.actiolayout.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.putExtra("type", "3");
            intent.putExtra("name", "活动消息");
            intent.setClass(MessageListActivity.this, MessageContentListActivity.class);
            startActivity(intent);
        });
    }

    private String getMessageStr(String message) {
        if (TextUtils.isEmpty(message)) {
            return "暂无消息";
        }
        return message;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.loadMessageCount();
    }
}