package com.example.leudaemialikeme.Adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Activity.BaseActivity;
import com.example.leudaemialikeme.Activity.BlogDetailActivity;
import com.example.leudaemialikeme.Model.Blog;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.OkHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BlogAdapter extends RecyclerView.Adapter<BlogAdapter.ViewHolder> {
    //    private List<Recnews> mRecnewsList;
    private List<Blog> mBlogList = new ArrayList<Blog>();
    private Blog blog;
//    private View view;
//    private Context context;
//    ViewHolder holder;

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView image;
        TextView theme;
        TextView intro;
        TextView visNum;
        TextView time;
        LinearLayout linear;
        TextView blog_id_view;
        TextView blog_likeNum_view;
        TextView blog_colNum_view;
        TextView blog_uid_view;
        TextView blog_content_view;

        public ViewHolder(View view){
            super(view);
            blog_likeNum_view=(TextView)view.findViewById(R.id.likeNum);
            blog_colNum_view=(TextView)view.findViewById(R.id.colNum);
            blog_id_view=(TextView)view.findViewById(R.id.bid);
            blog_uid_view=(TextView)view.findViewById(R.id.uid);
            image=(ImageView)view.findViewById(R.id.recNews_image);
            theme=(TextView)view.findViewById(R.id.recNews_theme);
            intro=(TextView)view.findViewById(R.id.recNews_intro);
            visNum=(TextView)view.findViewById(R.id.recNews_visNum);
            time=(TextView)view.findViewById(R.id.recNews_time);
            linear=(LinearLayout)view.findViewById(R.id.news);
            blog_content_view=(TextView)view.findViewById(R.id.content);
        }
    }

    public BlogAdapter(List<Blog> blogList){
        mBlogList=blogList;
//        Log.e("recAdapter中mBlogList的数据",String.valueOf(blogList));
    }

    public BlogAdapter(){

    }

    public void setRecnewsList(List<Blog> blogList){
        mBlogList=blogList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recnews_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
//        context=parent.getContext();

        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                UpdateBrowseNum(holder.blog_id_view.getText().toString());
                Intent intent=new Intent(view.getContext(), BlogDetailActivity.class);
//                intent.putExtra("from",String.valueOf(view.getContext()));
                intent.putExtra("bid",holder.blog_id_view.getText().toString());//新增bid
//                Log.e("传输前的bid",holder.blog_id_view.getText().toString());
                intent.putExtra("type",2);//帖子2
                intent.putExtra("bTitle",holder.theme.getText().toString());
                intent.putExtra("bContent",holder.blog_content_view.getText().toString());
                intent.putExtra("bColNum",holder.blog_colNum_view.getText().toString());
                intent.putExtra("bViewNum",String.valueOf(Integer.parseInt(holder.visNum.getText().toString())+1));
                intent.putExtra("uid",holder.blog_uid_view.getText().toString());

                view.getContext().startActivity(intent);

            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BlogAdapter.ViewHolder holder, int position) {
        blog=mBlogList.get(position);
//        holder.image.setImageResource(R.drawable.rec_news1);
        holder.visNum.setText(String.valueOf(blog.getBbrowse()));
        holder.intro.setText(blog.getBcontent());
        holder.theme.setText(blog.getBtittle());

        holder.time.setText(String.valueOf(blog.getBtime()).substring(0,19));
        holder.blog_id_view.setText(String.valueOf(blog.getBid()));
        holder.blog_colNum_view.setText(String.valueOf(blog.getBcollectNum()));
        holder.blog_likeNum_view.setText(String.valueOf(blog.getBlikeNum()));
        holder.blog_uid_view.setText(String.valueOf(blog.getUid()));
        holder.blog_content_view.setText(blog.getBcontent());

    }

    @Override
    public int getItemCount() {
        return mBlogList.size();
    }

    private void UpdateBrowseNum(String bid){
        String url = BaseActivity.SERVER_URL + "/browse-servlet?action=UpdateBrowse";
        Map<String,String> paramMap = new HashMap<String,String>();
        Log.e("获取浏览量的返回结果","浏览量");
        paramMap.put("id", bid);

//登录后需要将uid换为这部分
//        paramMap.put("uid", String.valueOf(BaseActivity.owner.getNetId()));
        paramMap.put("uid", String.valueOf(BaseActivity.owner.getNetId()));
//        只对帖子进行更新
        paramMap.put("type", String.valueOf(2));

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("获取浏览量的返回结果：uid",String.valueOf(1));
                String jsonStr = OkHttpUtil.synPost(url,paramMap);
                Log.e("浏览量的返回结果",jsonStr);
//                blog.setBbrowse(blog.getBbrowse()+1);
//                ((AppCompatActivity) context).runOnUiThread(new Runnable(){
//                    @Override
//                    public void run() {
////                        holder.visNum.setText(String.valueOf(blog.getBbrowse()));
//                    }
//                });
            }
        }).start();
    }
}
