package com.example.cloudmusic.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.cloudmusic.R;
import com.example.cloudmusic.been.SongListBeen;
import com.example.cloudmusic.adapter.SongListAdapter;
import com.example.cloudmusic.utils.OkHttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends Fragment {

    private Handler mHandler;
    private SongListAdapter adapter;
    private List<SongListBeen> recommendList;
    private View root;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.activity_main, container, false);
        initUI();
        return root;
    }

    @SuppressLint("HandlerLeak")
    private void initUI() {
        recommendList = new ArrayList<>();

        adapter = new SongListAdapter(recommendList, root.getContext());
        TextView shit = root.findViewById(R.id.horizon_title);
        shit.setText("推荐歌单");
        RecyclerView recycler = root.findViewById(R.id.horizon_recycler);
        LinearLayoutManager manager = new LinearLayoutManager(root.getContext(), RecyclerView.HORIZONTAL, false);
        recycler.setAdapter(adapter);
        recycler.setLayoutManager(manager);
        initRequest("http://192.168.86.158:3000/personalized?limit=10",1);
        mHandler = new Handler(){
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        recommendList.addAll((Collection<? extends SongListBeen>) msg.obj);
                        adapter.notifyDataSetChanged();
                      //  Looper.loop();
                }
            }
        };
    }

    private void initRequest(String url,int msg) {
        List<SongListBeen> list = new ArrayList<>();
        OkHttpUtil okHttpUtil = OkHttpUtil.getInstance();
        okHttpUtil.sendGetRequest(url, new OkHttpUtil.OkhttpInterface() {
            @Override
            public void success(Call call, Response response) {
                try {
                    String body = response.body().string();
                    JSONObject res = new JSONObject(body);
                    JSONArray result = res.optJSONArray("result");
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject json = (JSONObject) result.get(i);
                        String name = json.optString("name");
                        String id = json.optString("id");
                        String picUrl = json.optString("picUrl");
                        String playCount = json.optString("playCount");
                        SongListBeen been = new SongListBeen(name, picUrl, id, playCount);
                        list.add(been);
                    }
                    Message message = new Message();
                    message.obj = list;
                    message.what = msg;
                    mHandler.sendMessage(message);
                  //  mHandler.getLooper().quit();
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void failure(Call call, IOException e) {
                e.printStackTrace();
                Log.d("failure: ", e.getMessage());
            }
        });

    }
}