package com.virjar.echo.adr.ui;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.virjar.echo.adr.EchoApplication;
import com.virjar.echo.adr.R;
import com.virjar.echo.adr.bean.CommonRes;
import com.virjar.echo.adr.repo.APIServices;
import com.virjar.echo.adr.util.MainThreadRunning;

import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MineFragment extends Fragment {

    private MyAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.rc_port_list_view);

        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 10, true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);
        return view;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.tv_port);
        }

    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        private List<Integer> resIds = Collections.emptyList();

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_port, parent, false);
            return new MyViewHolder(item);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            holder.imageView.setText(String.valueOf(resIds.get(position)));
        }

        @Override
        public int getItemCount() {
            return resIds.size();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        reloadProxies();
    }

    private void reloadProxies() {
        APIServices.userApi
                .myProxies().enqueue(new Callback<CommonRes<List<Integer>>>() {
            @Override
            public void onResponse(Call<CommonRes<List<Integer>>> call, final Response<CommonRes<List<Integer>>> response) {
                MainThreadRunning.runOnMainThread(new Runnable() {
                    @Override
                    public void run() {
                        CommonRes<List<Integer>> body = response.body();
                        if (body == null) {
                            Toast.makeText(EchoApplication.echoApplication,
                                    "代理列表加载失败,数据解析异常", Toast.LENGTH_LONG).show();
                            return;
                        }
                        if (!body.isOk()) {
                            Toast.makeText(EchoApplication.echoApplication,
                                    body.getMessage(), Toast.LENGTH_LONG).show();
                            return;
                        }
                        List<Integer> data = body.getData();
                        myAdapter.resIds = data;
                        myAdapter.notifyDataSetChanged();
                    }
                });

            }

            @Override
            public void onFailure(Call<CommonRes<List<Integer>>> call, Throwable t) {
                MainThreadRunning.runOnMainThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(EchoApplication.echoApplication,
                                "代理列表加载失败", Toast.LENGTH_LONG).show();
                    }
                });

            }
        });
    }

}
