package com.navinfo.outdoor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.activity.WebActivity;
import com.navinfo.outdoor.adapter.MessageAdapter;
import com.navinfo.outdoor.adapter.TaskPrefectureAdapter;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.TaskPrefectureBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;

/**
 * 寻宝-消息的Fragment
 */
public class MessageFragment extends BaseFragment implements View.OnClickListener {

    private ImageView messageFinal;
    private XRecyclerView messageRecycler;
    private MessageAdapter messageAdapter;
    private TextView tvRoad;

    public static MessageFragment newInstance(Bundle bundle) {
        MessageFragment fragment = new MessageFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {
        super.initView();
        messageFinal = (ImageView) findViewById(R.id.message_final);
        messageFinal.setOnClickListener(this::onClick);
        messageRecycler = (XRecyclerView) findViewById(R.id.message_recycler);
        messageRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        tvRoad = findViewById(R.id.tv_read);
        tvRoad.setOnClickListener(this::onClick);
        messageRecycler.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        messageRecycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        messageRecycler.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消上拉加载，刷新功能。
        messageRecycler.setPullRefreshEnabled(false);
        messageRecycler.setLoadingMoreEnabled(false);
        messageAdapter = new MessageAdapter(getContext());
        messageRecycler.setAdapter(messageAdapter);
        messageAdapter.setMessageClickItem(new MessageAdapter.messageClickItem() {
            @Override
            public void item(int id) {
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
        messageRecycler.getDefaultFootView().setNoMoreHint("已全部加载完毕");

        messageRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
    }
    private void initNetwork() {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "5");
        httpParams.put("pageNum", "1");
        httpParams.put("pageSize", "10");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.MSG_LISt)
                .cls(TaskPrefectureBean.class)
                .params(httpParams)
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<TaskPrefectureBean>() {
                    @Override
                    public void onSuccess(TaskPrefectureBean taskPrefectureBean, int id) {
                        dismissLoadingDialog();
                        if (taskPrefectureBean.getCode() == 200){
                            messageAdapter.setMessageList(taskPrefectureBean.getBody().getList());
                        }else {
                            Toast.makeText(getActivity(), taskPrefectureBean.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }


                        Log.d("TAG", "onSuccess: " + taskPrefectureBean.getMessage() + "");
                        //initTaskSpecification(taskSpecificationBean,start);
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }
    @Override
    protected void initData() {
        super.initData();
        initNetwork();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.message_final:
                getActivity().finish();
                break;

            case R.id.tv_read:

                break;
        }
    }
}