package com.example.leudaemialikeme.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Adapter.HistoryQuestionAdapter;
import com.example.leudaemialikeme.Adapter.QuestionAdapter;
import com.example.leudaemialikeme.Model.Question;
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.util.ArrayList;
import java.util.List;
import java.util.Map;

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

public class ConcernedProblemActivity extends BaseActivity {

    private List<Question> mData = new ArrayList<>();
    private RecyclerView recyclerView;
    private boolean isPause;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concerned_problem);
        recyclerView = (RecyclerView) findViewById(R.id.questionList);//获得视图
        ImageView back_concerned = findViewById(R.id.back_concerned);
        LinearLayoutManager layoutManager;
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);//建立线性布局
        QuestionAdapter adapter = new QuestionAdapter(mData, ConcernedProblemActivity.this);//创建适配器
        recyclerView.setAdapter(adapter);//将视图与适配器连接起来
        getData();//获得数据
        back_concerned.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = ConcernedProblemActivity.SERVER_URL+"/my-servlet?action=concernedQuestion&uid="+ BaseActivity.owner.getNetId()+"&type="+3;
                    Callback callback = new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            System.out.println("-----ERROR----");
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String jsonStr = response.body().string();
                            Gson gson = new Gson();
                            Map<String, String> jsonMap= gson.fromJson(jsonStr,  new TypeToken<Map<String, String>>() {}.getType());
                            final String message=(String) jsonMap.get("message");
                            if (message.equals("success")) {
                                List<Question> questionList = gson.fromJson(jsonMap.get("questionList"), new TypeToken <List<Question>>(){}.getType());
                                mData = questionList;
                                QuestionAdapter adapter = new QuestionAdapter(mData, ConcernedProblemActivity.this);//创建适配器
                                recyclerView.setAdapter(adapter);
                            }
                        }
                    };
                    OkHttpUtil.asyGet(url,callback);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

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

    @Override
    public void onResume() {
        super.onResume();
        if (isPause){ //判断是否暂停
            isPause = false;
            getData();
            HistoryQuestionAdapter adapter = new HistoryQuestionAdapter(mData, ConcernedProblemActivity.this);//创建适配器
            recyclerView.setAdapter(adapter);
        }

    }
}
