package com.qhclh.ytzh.index;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.NewsBean;
import com.qhclh.ytzh.index.planorder.WarningActivity;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 青花瓷 on 2017/12/25.
 */

public class JPInfoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.system_pic)
    ImageView system_pic;
    @BindView(R.id.system_title)
    TextView system_title;
    @BindView(R.id.system_content)
    TextView system_content;
    @BindView(R.id.system_time)
    TextView system_time;
    @BindView(R.id.system_red)
    TextView system_red;

    @BindView(R.id.warninginfo_pic)
    ImageView warninginfo_pic;
    @BindView(R.id.warninginfo_title)
    TextView warninginfo_title;
    @BindView(R.id.warninginfo_content)
    TextView warninginfo_content;
    @BindView(R.id.warninginfo_time)
    TextView warninginfo_time;
    @BindView(R.id.warninginfo_red)
    TextView warninginfo_red;

    @BindView(R.id.chickscan_pic)
    ImageView chickscan_pic;
    @BindView(R.id.chickscan_title)
    TextView chickscan_title;
    @BindView(R.id.chickscan_content)
    TextView chickscan_content;
    @BindView(R.id.chickscan_time)
    TextView chickscan_time;
    @BindView(R.id.chickscan_red)
    TextView chickscan_red;

    @BindView(R.id.killc_pic)
    ImageView killc_pic;
    @BindView(R.id.killc_title)
    TextView killc_title;
    @BindView(R.id.killc_content)
    TextView killc_content;
    @BindView(R.id.killc_time)
    TextView killc_time;
    @BindView(R.id.killc_red)
    TextView killc_red;

    @BindView(R.id.system_ll)
    LinearLayout system_ll;
    @BindView(R.id.warninginfo_ll)
    LinearLayout warninginfo_ll;
    @BindView(R.id.chickscan_ll)
    LinearLayout chickscan_ll;
    @BindView(R.id.killc_ll)
    LinearLayout killc_ll;

    private List<NewsBean> newsBeanListsystem;
    private List<NewsBean> newsBeanListwarning;
    private List<NewsBean> newsBeanListchick;
    private List<NewsBean> newsBeanListkillc;

    @Override
    protected int setLayoutId() {
        return R.layout.act_jpinfo;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "消息", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        newsBeanListsystem = DataSupport.select("id", "url", "title", "content", "time", "type","red").where("type = ?","01").order("time desc").find(NewsBean.class);
        newsBeanListwarning = DataSupport.select("id", "url", "title", "content", "time", "type","red").where("type = ?","02").order("time desc").find(NewsBean.class);
        newsBeanListchick = DataSupport.select("id", "url", "title", "content", "time", "type","red").where("type = ?","03").order("time desc").find(NewsBean.class);
        newsBeanListkillc = DataSupport.select("id", "url", "title", "content", "time", "type","red").where("type = ?","04").order("time desc").find(NewsBean.class);
//        System.out.println("aaa11++++++"+newsBeanListsystem.toString());
//        System.out.println("aaa22++++++"+newsBeanListwarning.toString());
//        System.out.println("aaa33++++++"+newsBeanListchick.toString());
//        System.out.println("aaa44++++++"+newsBeanListkillc.toString());
        if (newsBeanListsystem!=null && newsBeanListsystem.size()>0) {
            Glide.with(this).load(newsBeanListsystem.get(0).getUrl()).placeholder(R.mipmap.ic_launcher_round).error(R.mipmap.ic_launcher_round).into(system_pic);
            system_title.setText("系统提示");
            system_content.setText(newsBeanListsystem.get(0).getContent());
            system_time.setText(newsBeanListsystem.get(0).getTime());
            if (newsBeanListsystem.get(0).getRed() == 1) {
                system_red.setVisibility(View.VISIBLE);
            }else {
                system_red.setVisibility(View.INVISIBLE);
            }
        }else {
            system_ll.setVisibility(View.GONE);
        }

        if (newsBeanListwarning!=null && newsBeanListwarning.size()>0) {
            Glide.with(this).load(newsBeanListwarning.get(0).getUrl()).placeholder(R.mipmap.ic_launcher_round).error(R.mipmap.ic_launcher_round).into(warninginfo_pic);
            warninginfo_title.setText("预警提示");
            warninginfo_content.setText(newsBeanListwarning.get(0).getContent());
            warninginfo_time.setText(newsBeanListwarning.get(0).getTime());
            if (newsBeanListwarning.get(0).getRed() == 1) {
                warninginfo_red.setVisibility(View.VISIBLE);
            }else {
                warninginfo_red.setVisibility(View.INVISIBLE);
            }
        }else {
            warninginfo_ll.setVisibility(View.GONE);
        }

        if (newsBeanListchick!=null && newsBeanListchick.size()>0) {
            Glide.with(this).load(newsBeanListchick.get(0).getUrl()).placeholder(R.mipmap.ic_launcher_round).error(R.mipmap.ic_launcher_round).into(chickscan_pic);
            chickscan_title.setText("鸡苗扫码推送");
            chickscan_content.setText(newsBeanListchick.get(0).getContent());
            chickscan_time.setText(newsBeanListchick.get(0).getTime());
            if (newsBeanListchick.get(0).getRed() == 1) {
                chickscan_red.setVisibility(View.VISIBLE);
            }else {
                chickscan_red.setVisibility(View.INVISIBLE);
            }
        }else {
            chickscan_ll.setVisibility(View.GONE);
        }

        if (newsBeanListkillc!=null && newsBeanListkillc.size()>0) {
            Glide.with(this).load(newsBeanListkillc.get(0).getUrl()).placeholder(R.mipmap.ic_launcher_round).error(R.mipmap.ic_launcher_round).into(killc_pic);
            killc_title.setText("屠宰单");
            killc_content.setText(newsBeanListkillc.get(0).getContent());
            killc_time.setText(newsBeanListkillc.get(0).getTime());
            if (newsBeanListkillc.get(0).getRed() == 1) {
                killc_red.setVisibility(View.VISIBLE);
            }else {
                killc_red.setVisibility(View.INVISIBLE);
            }
        }else {
            killc_ll.setVisibility(View.GONE);
        }
    }

    private Intent intent;
    @OnClick({R.id.system_ll, R.id.warninginfo_ll, R.id.chickscan_ll,R.id.killc_ll})
    public void onClick(View view) {
        intent = new Intent(JPInfoActivity.this, WarningActivity.class);
        switch (view.getId()) {

            ///< 系统消息
            case R.id.system_ll: {
                intent.putExtra("type","01");
                break;
            }
            ///< 预警消息
            case R.id.warninginfo_ll: {
                intent.putExtra("type","02");
                break;
            }

            ///< 扫描消息
            case R.id.chickscan_ll: {
                intent.putExtra("type","03");
                break;
            }
            ///< 屠宰消息
            case R.id.killc_ll: {
                intent.putExtra("type","04");
                break;
            }
        }
        startActivity(intent);
    }
}
