package com.zhilin.patrol.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.zhilin.patrol.R;
import com.zhilin.patrol.bean.LoginEntity;
import com.zhilin.patrol.bean.ReconsiderActivityEntity;
import com.zhilin.patrol.bean.RectifyTaskEntity;
import com.zhilin.patrol.http.HttpUrl;
import com.zhilin.patrol.tool.DensityUtil;
import com.zhilin.patrol.tool.DialogUtil;
import com.zhilin.patrol.tool.JsonUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;


public class RectifyTaskActivity extends BaseActivity {

    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    MaterialRefreshLayout mRefreshLayout;
    private boolean mIsFirst;

    private LoginEntity.InfoListBean.UserInfoBean mUserInfo;
    private List<RectifyTaskEntity.InfoListBean> mList;
//    private List<RectifyTaskImgEntity.InfoListBean> mgList;
    private CommonAdapter<RectifyTaskEntity.InfoListBean> mAdapter;
//    private CommonAdapter<RectifyTaskImgEntity.InfoListBean> mgAdapter;
    private int statusInt=1;
    private int mPage = 1;
    private int mType = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_task);
        ButterKnife.bind(this);
        mUserInfo = (LoginEntity.InfoListBean.UserInfoBean) getIntent().getSerializableExtra("userInfo");
        mType=getIntent().getIntExtra("mType",1);
        if(mType==1){
            setTitle("整改任务");
        }else if(mType==3){
            setTitle("整改不通过");
        }else if(mType==2){
            setTitle("复议被驳回");
        }

        init();
        showLoadingView();
        mIsFirst = true;
        request();
    }

    @Override
    public void request() {
        Log.i("code==",mUserInfo.getUserCode());
        Log.i("mType",mType+"");
        if(mType==1){
            OkHttpUtils.post()
                    .url(HttpUrl.RECTIFY_TASK)
//                .url(HttpUrl.CHECK_TASK)
                    .addParams("uCode",mUserInfo.getUserCode())
                    .addParams("status",statusInt+"")
                    .build().execute(new Callback<RectifyTaskEntity>() {
                @Override
                public RectifyTaskEntity parseNetworkResponse(Response response, int id) throws Exception {
                    String jsonStr = response.body().string();
                    return (RectifyTaskEntity) JsonUtils.parseJson2Obj(jsonStr, RectifyTaskEntity.class);
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    if (mIsFirst) {
                        showFailView();
                    } else {
                        mRefreshLayout.finishRefresh();
                        mRefreshLayout.finishRefreshLoadMore();
                    }
                    DialogUtil.showNetFailDialog(RectifyTaskActivity.this);
                }

                @Override
                public void onResponse(RectifyTaskEntity entity, int id) {

                    if (mIsFirst) {
                        showContentView();
                    } else {
                        mRefreshLayout.finishRefresh();
                        mRefreshLayout.finishRefreshLoadMore();
                    }
                    mIsFirst = false;
                    if (entity == null) {
                        DialogUtil.showErrDialog(RectifyTaskActivity.this, "没有任务");
                        return;
                    }
                    List<RectifyTaskEntity.InfoListBean> list = entity.getInfoList();

                    if (!"1000".endsWith(entity.getReturnCode())) {
                        DialogUtil.showErrDialog(RectifyTaskActivity.this, entity.getReturnMsg());
                        return;
                    }
                    if (list == null) {
                        mRefreshLayout.setLoadMore(false);
                        return;
                    }
                    if (list.size() < 10) {
                        mRefreshLayout.setLoadMore(false);
                    }
                    if (mPage == 1) {
                        mList.clear();
                    }
                    mPage++;
                    mList.addAll(list);
                    mAdapter.notifyDataSetChanged();
                    if (mList.isEmpty()) {
                        showEmptyView();
                    }
                }
            });
        }
        else if(mType==3){
            OkHttpUtils.post()
                    .url(HttpUrl.RECTIFY_TASK)
//                    .url(HttpUrl.RECHECK_TASK)
                    .addParams("uCode", mUserInfo.getUserCode())
                    .addParams("status", 4 + "")
                    .build().execute(new Callback<RectifyTaskEntity>() {
                @Override
                public RectifyTaskEntity parseNetworkResponse(Response response, int id) throws Exception {
                    String jsonStr = response.body().string();
                    return (RectifyTaskEntity) JsonUtils.parseJson2Obj(jsonStr, RectifyTaskEntity.class);
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    if (mIsFirst) {
                        showFailView();
                    } else {
                        mRefreshLayout.finishRefresh();
                        mRefreshLayout.finishRefreshLoadMore();
                    }
                    DialogUtil.showNetFailDialog(RectifyTaskActivity.this);
                }

                @Override
                public void onResponse(RectifyTaskEntity entity, int id) {

                    if (mIsFirst) {
                        showContentView();
                    } else {
                        mRefreshLayout.finishRefresh();
                        mRefreshLayout.finishRefreshLoadMore();
                    }
                    mIsFirst = false;
                    if (entity == null) {
                        DialogUtil.showErrDialog(RectifyTaskActivity.this, "没有任务");
                        return;
                    }
                    List<RectifyTaskEntity.InfoListBean> list = entity.getInfoList();

                    if (!"1000".endsWith(entity.getReturnCode())) {
                        DialogUtil.showErrDialog(RectifyTaskActivity.this, entity.getReturnMsg());
                        return;
                    }
                    if (list == null) {
                        mRefreshLayout.setLoadMore(false);
                        return;
                    }
                    if (list.size() < 10) {
                        mRefreshLayout.setLoadMore(false);
                    }
                    if (mPage == 1) {
                        mList.clear();
                    }
                    mPage++;
                    mList.addAll(list);
                    mAdapter.notifyDataSetChanged();
                    if (mList.isEmpty()) {
                        showEmptyView();
                    }
                }
            });
        }
    }

    private void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        mList = new ArrayList<>();
//        mgList=new ArrayList<>();ArrayList
//        if(mType==1){
            mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                    .color(getResources().getColor(R.color.line_gray))
                    .size(DensityUtil.dip2px(this, 1))
                    .build());
            mAdapter = new CommonAdapter<RectifyTaskEntity.InfoListBean>(this, R.layout.item_rectify_task, mList) {//item_rectify_task   item_type_task
                @Override
                protected void convert(ViewHolder holder, final RectifyTaskEntity.InfoListBean bean, int position) {
                    holder.setText(R.id.tv_add, bean.getCName());
                    Log.i("address==", bean.getCName());
                    Log.i("status==", bean.getRectify_status());
                    String StatesStr = "";
                    // status=0 待审核 = 1已驳回 = 2已通过 null未提交
                    if (bean.getRectify_status().equals("-1") && bean.getRecheck_status().equals("-1")) {
                        StatesStr = "未提交";
                        holder.setVisible(R.id.btn_to_rl_upload2, true);
                        holder.setVisible(R.id.btn_to_rl_upload, true);
                    } else if (bean.getRecheck_status().equals("0")) {
                        StatesStr = "复议待审核";
                        holder.setVisible(R.id.btn_to_rl_upload2, false);
                        holder.setVisible(R.id.btn_to_rl_upload, false);
                    } else if (bean.getRecheck_status().equals("1") && bean.getRectify_status().equals("-1")) {
                        StatesStr = "复议已驳回";
                        holder.setVisible(R.id.btn_to_rl_upload2, true);
                        holder.setVisible(R.id.btn_to_rl_upload, false);
                    } else if (bean.getRectify_status().equals("1") && bean.getRecheck_status().equals("-1")) {
                        StatesStr = "不通过";
                        holder.setVisible(R.id.btn_to_rl_upload2, false);
                        holder.setVisible(R.id.btn_to_rl_upload, false);
                    } else if (bean.getRectify_status().equals("1") && bean.getRecheck_status().equals("1")) {
                        StatesStr = "不通过";
                        holder.setVisible(R.id.btn_to_rl_upload2, false);
                        holder.setVisible(R.id.btn_to_rl_upload, false);
                    }
                    final ArrayList imgList=bean.getUrls();
                    Log.i("imgList----/", String.valueOf(imgList));
                    holder.setText(R.id.tv_check, StatesStr);
                    holder.setText(R.id.tv_question, " • " + bean.getQuestion());
                    holder.setOnClickListener(R.id.btn_to_rl_upload, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String text = " • " + bean.getQuestion();
                            toActivityRecify(RecifyUploadActivity.class, mUserInfo, bean.getPbCode(), bean.getCName(), text, 2,imgList);
                        }
                    });
                    holder.setOnClickListener(R.id.btn_to_rl_upload2, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String text = " • " + bean.getQuestion();
                            toActivityRecify(RecifyUploadActivity.class, mUserInfo, bean.getPbCode(), bean.getCName(), text, 1,imgList);
                        }
                    });
                }
            };
            mRecyclerView.setAdapter(mAdapter);
//        }else{
//            mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
//                    .color(getResources().getColor(R.color.line_gray))
//                    .size(DensityUtil.dip2px(this, 1))
//                    .build());
//            mAdapter = new CommonAdapter<RectifyTaskEntity.InfoListBean>(this, R.layout.item_rectify_task, mList) {//item_rectify_task   item_type_task
//                @Override
//                protected void convert(ViewHolder holder, final RectifyTaskEntity.InfoListBean bean, int position) {
//                    holder.setText(R.id.tv_add, bean.getCName());
//                    Log.i("address==", bean.getCName());
//                    Log.i("status==", bean.getRectify_status());
//                    String StatesStr = "";
//                    // status=0 待审核 = 1已驳回 = 2已通过 null未提交
//                        holder.setVisible(R.id.btn_to_rl_upload2, true);
//                        holder.setVisible(R.id.btn_to_rl_upload, true);
//
//                    holder.setText(R.id.tv_check, StatesStr);
//                    holder.setText(R.id.tv_question, " • " + bean.getQuestion());
////                    holder.setOnClickListener(R.id.btn_to_rl_upload, new View.OnClickListener() {
////                        @Override
////                        public void onClick(View view) {
////                            String text = " • " + bean.getQuestion();
////                            toActivityRecify(RecifyUploadActivity.class, mUserInfo, bean.getPbCode(), bean.getCName(), text, 2);
////                        }
////                    });
////                    holder.setOnClickListener(R.id.btn_to_rl_upload2, new View.OnClickListener() {
////                        @Override
////                        public void onClick(View view) {
////                            String text = " • " + bean.getQuestion();
////                            toActivityRecify(RecifyUploadActivity.class, mUserInfo, bean.getPbCode(), bean.getCName(), text, 1);
////                        }
////                    });
//                }
//            };
//            mRecyclerView.setAdapter(mAdapter);
//        }

        mRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                mPage = 1;
                mRefreshLayout.setLoadMore(true);
                request();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                //load more refreshing...
                request();
            }
        });
    }


}
