package com.qhclh.ytzh.index;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
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.ArrayList;
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;

    private List<NewsBean> newsBeanListsystem;
    private List<NewsBean> newsBeanList;

    @BindView(R.id.jpcontent)
    LinearLayout jpcontent;

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

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

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

    private ImageView system_pic;
    private TextView system_title;
    private TextView system_content;
    private TextView system_time;
    private TextView system_red;

    private LinearLayout system_ll;

    private List<String> typeList;

    @Override
    protected void onResume() {
        super.onResume();
        getTypelist();
        for (int j = 0; j < typeList.size(); j++) {
            newsBeanList = DataSupport.select("id", "url", "title", "content", "time", "type", "red").where("type = ?", typeList.get(j)).order("time desc").find(NewsBean.class);
            View view = LayoutInflater.from(this).inflate(R.layout.view_jp, null);
            system_pic = view.findViewById(R.id.system_pic);
            system_title = view.findViewById(R.id.system_title);
            system_content = view.findViewById(R.id.system_content);
            system_time = view.findViewById(R.id.system_time);
            system_red = view.findViewById(R.id.system_red);
            system_ll = view.findViewById(R.id.system_ll);

            system_title.setText(newsBeanList.get(0).getTitle());
            system_content.setText(newsBeanList.get(0).getContent());
            system_time.setText(newsBeanList.get(0).getTime());
            if (newsBeanList.get(0).getRed() == 1) {
                system_red.setVisibility(View.VISIBLE);
            } else {
                system_red.setVisibility(View.INVISIBLE);
            }

            system_ll.setTag(newsBeanList.get(0).getType());
            system_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(JPInfoActivity.this, WarningActivity.class);
                    intent.putExtra("type", view.getTag().toString());
                    startActivity(intent);
                }
            });

            jpcontent.addView(view);

        }
    }

    private List<String> getTypelist() {
        newsBeanListsystem = DataSupport.select("id", "url", "title", "content", "time", "type", "red").order("time desc").find(NewsBean.class);

        typeList = new ArrayList<>();
        if (newsBeanListsystem != null && newsBeanListsystem.size() > 0) {
            for (int i = 0; i < newsBeanListsystem.size(); i++) {

                typeList.add(newsBeanListsystem.get(i).getType());
                for (int j = 0; j < typeList.size() - 1; j++) {
                    for (int k = typeList.size() - 1; k > j; k--) {
                        if (typeList.get(k).equals(typeList.get(j))) {
                            typeList.remove(k);
                        }
                    }
                }
            }
        }
        return typeList;
    }


    @Override
    protected void onPause() {
        super.onPause();
        jpcontent.removeAllViews();
    }
}
