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.MyQuestionAdapter;
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 okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyQuestionActivity extends BaseActivity {

    private List<Question> questionList = new ArrayList<>();
    RecyclerView recycleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_question);
        recycleView = findViewById(R.id.my_question_list);//获得视图
        LinearLayoutManager layoutManager;
        layoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(layoutManager);//建立线性布局
        MyQuestionAdapter adapter = new MyQuestionAdapter(questionList, MyQuestionActivity.this);//创建适配器
        recycleView.setAdapter(adapter);//将视图与适配器连接起来
        getMyQustionList();
        ImageView my_question_back = findViewById(R.id.my_question_back);
        my_question_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getMyQustionList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = LoginActivity.SERVER_URL+"/my-servlet?action=getMyQuestionList&iduser="+ BaseActivity.owner.getNetId();
                    OkHttpUtil.asyGet(url, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String jsonStr = response.body().string();
                            Gson gson = new Gson();
                            questionList = gson.fromJson(jsonStr, new TypeToken<List<Question>>() {}.getType());
                            runOnUiThread(new Runnable(){
                                @Override
                                public void run() {
                                    MyQuestionAdapter adapter = new MyQuestionAdapter(questionList, MyQuestionActivity.this);//创建适配器
                                    recycleView.setAdapter(adapter);//将视图与适配器连接起来
                                }
                            });
                        }
                    });

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
