package com.example.leudaemialikeme.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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.MsgAdapter;
import com.example.leudaemialikeme.Model.Chat;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.service.ChatService;

import java.util.ArrayList;
import java.util.List;

public class MessageChannelFragment extends Fragment {
    private static final String ARG_CATEGORY_TITLE = "category_title";
    private String messageCategoryTitle = "Default";
    private TextView mTitleField;
    private List<Chat> chatList = new ArrayList<Chat>();
    private RecyclerView recyclerView;

    public MessageChannelFragment() {
        // Required empty public constructor
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            messageCategoryTitle = (String) 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 (messageCategoryTitle.equals("        私信      ")) {
            view = inflater.inflate(R.layout.fragment_private_msg,container,false);
            recyclerView = view.findViewById(R.id.list_private_msg);
            System.out.println("oncreateview:"+recyclerView);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManager);
            Log.d("记录","调用方法获取消息列表");
            getMsgList();
            MsgAdapter msgAdapter = new MsgAdapter(chatList);
            recyclerView.setAdapter(msgAdapter);

        } else if (messageCategoryTitle.equals("       评论       ")) {
            view = inflater.inflate(R.layout.fragment_comment, container, false);
        } else if (messageCategoryTitle.equals("       赞      ")) {
            view = inflater.inflate(R.layout.fragment_nice, container, false);
        } else if (messageCategoryTitle.equals("       粉丝       ")) {
            view = inflater.inflate(R.layout.fragment_fans, container, false);
        } else {
            view = inflater.inflate(R.layout.fragment_message_channel, container, false);
            mTitleField = (TextView) view.findViewById(R.id.messageCategoryTitle);
            mTitleField.setText(messageCategoryTitle);
        }
        return view;
    }

    private void getMsgList(){
        ChatService chatService = new ChatService();
        chatList = chatService.findChatList(BaseActivity.owner.getNetId());
        Log.d("chatlist:", String.valueOf(chatList.size()));
    }
//    @Override
//    public void onResume() {
//        System.out.println("-----------------------");
//        super.onResume();
//        System.out.println("-----------------------");
//        getMsgList();
//        MsgAdapter adapter = new MsgAdapter(chatList);
//        System.out.println("onresume:" + recyclerView);
//        recyclerView.setAdapter(adapter);
//        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//    }
    public void updateUi(){
        getMsgList();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MsgAdapter adapter = new MsgAdapter(chatList);
                recyclerView.setAdapter(adapter);
            }
        });
    }
    public String getMessageCategoryTitle(){
        return messageCategoryTitle;
    }
}
