package com.example.leudaemialikeme.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Adapter.BlogAdapter;
import com.example.leudaemialikeme.Model.Blog;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.OkHttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BlogSearchResultActivity extends AppCompatActivity {
    ImageView blogSearchBackView;
    TextView blogSearchTextView;
    TextView blogResultBackView;
    RecyclerView resultRecyclerView;
    List<Blog> resultBlog=new ArrayList<>();
    private boolean isPause = false;
    String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_search_result);
        Intent intent=getIntent();
        text=intent.getStringExtra("text");
        blogSearchTextView=(TextView) findViewById(R.id.blog_search_text);
        blogSearchTextView.setText(text);
        Log.e("text",text);
        resultRecyclerView=(RecyclerView) findViewById(R.id.result_recyclerView);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        resultRecyclerView.setLayoutManager(layoutManager);
        BlogAdapter resultAdapter = new BlogAdapter();

        initView();
        initData(text);
        initClick();

        resultAdapter.setRecnewsList(resultBlog);
        resultRecyclerView.setAdapter(resultAdapter);
    }
    private void initView(){
        blogSearchBackView=(ImageView) findViewById(R.id.blog_search_back);
        blogResultBackView=(TextView)findViewById(R.id.blog_result_back);
    }
    private void initData(String text){

        Map<String,String> paramMap=new HashMap<String, String>();
        paramMap.put("text",text);
//        Log.e("initData中的text",text);
        String url=BaseActivity.SERVER_URL+"/blog-servlet?action=getSearchResult";
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtil.asyPost(url, paramMap, new Callback(){

                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Gson gson = new Gson();
                        String jsonStr = response.body().string();
                        Log.e("result jsonStr",jsonStr);
                        Type listType = new TypeToken<List<Blog>>() {}.getType();
                        List<Blog> resultList = gson.fromJson(jsonStr, listType);
                        resultBlog = resultList ;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                BlogAdapter resultAdapter = new BlogAdapter();
                                resultAdapter.setRecnewsList(resultList);
                                resultRecyclerView.setAdapter(resultAdapter);
                            }
                        });
                    }
                });
            }
        }).start();
    }

    private void initClick() {
        blogSearchBackView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        blogResultBackView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        isPause = true; //记录页面已经被暂停
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isPause) { //判断是否暂停
            isPause = false;
            Map<String, String[]> paramMap = new HashMap<String, String[]>();
            initData(text);
        }
    }
}