package com.example.leudaemialikeme.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Activity.BaseActivity;
import com.example.leudaemialikeme.Adapter.MyAnswerAdapter;
import com.example.leudaemialikeme.Adapter.MyBlogAdapter;
import com.example.leudaemialikeme.Model.Blog;
import com.example.leudaemialikeme.Model.MyAnswerItemView;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.OkHttpUtil;
import com.example.leudaemialikeme.Utils.TimeUtil;
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 APNChannelFragment extends Fragment {
    private static final String ARG_CATEGORY_TITLE = "category_title";
    private String APNCategoryTitle = "Default";
    private List<MyAnswerItemView> answerList = new ArrayList<>();
    private List<Blog> blogList = new ArrayList<>();
    private RecyclerView answerRecyclerview;
    private RecyclerView blogRecyclerview;
    public APNChannelFragment() {
        // Required empty public constructor
    }

    public static APNChannelFragment newInstance(String APNCategoryTitle) {
        APNChannelFragment fragment = new APNChannelFragment();
        Bundle args = new Bundle();
        args.putString(ARG_CATEGORY_TITLE, APNCategoryTitle);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            APNCategoryTitle = getArguments().getString(ARG_CATEGORY_TITLE);
        } catch (java.lang.NullPointerException e) {
            System.out.println("TesFragment getArg error!");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view;
        if (APNCategoryTitle.equals("回答")) {
            view = inflater.inflate(R.layout.recyclerview, container, false);
            answerRecyclerview = view.findViewById(R.id.list_community);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            answerRecyclerview.setLayoutManager(layoutManager);
            MyAnswerAdapter adapter = new MyAnswerAdapter(answerList, getContext());
            answerRecyclerview.setAdapter(adapter);
            getMyAnswerList();
        } else if (APNCategoryTitle.equals("帖子")) {
            view = inflater.inflate(R.layout.recyclerview, container, false);
            blogRecyclerview = view.findViewById(R.id.list_community);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            blogRecyclerview.setLayoutManager(layoutManager);
            MyBlogAdapter adapter = new MyBlogAdapter(blogList);
            blogRecyclerview.setAdapter(adapter);
            getMyBlogList();
        } else {
            view = inflater.inflate(R.layout.fragment_a_p_n_channel, container, false);
        }
        return view;
    }

    private void getMyBlogList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = BaseActivity.SERVER_URL+"/my-servlet?action=getMyBlogList&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();
                            List<Blog> blogList = gson.fromJson(jsonStr, new TypeToken<List<Blog>>() {}.getType());
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    MyBlogAdapter adapter = new MyBlogAdapter(blogList);
                                    blogRecyclerview.setAdapter(adapter);
                                }
                            });
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void getMyAnswerList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = BaseActivity.SERVER_URL+"/my-servlet?action=getMyAnswerList&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();
                            List<Map<String,String>> list = gson.fromJson(jsonStr, new TypeToken<List<Map<String, String>>>() {}.getType());
                            for(int i=0; i<list.size(); i++){
                                Map<String, String> jsonMap = list.get(i);
                                MyAnswerItemView myAnswerItemView = new MyAnswerItemView();
                                myAnswerItemView.setQid(Integer.parseInt(jsonMap.get("qid")));
                                myAnswerItemView.setQtitle(jsonMap.get("qtitle"));
                                myAnswerItemView.setAcontent(jsonMap.get("content"));
                                myAnswerItemView.setAnswerTime(TimeUtil.stringToTime(jsonMap.get("answerTime")));
                                answerList.add(myAnswerItemView);
                            }
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    MyAnswerAdapter adapter = new MyAnswerAdapter(answerList, getContext());
                                    answerRecyclerview.setAdapter(adapter);
                                }
                            });
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}