package com.rv.rvmine.traveler;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.frame.base.url.Constance;
import com.frame.rv.config.RvFrameConfig;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.rv.rvmine.R2;
import com.rv.home.rv.module.ApiConfig;
import com.rv.home.rv.module.basic.BaseStatusActivity;
import com.rv.home.rv.module.basic.presenter.CommonPresenter;
import com.rv.rvmine.R;
import com.rv.rvmine.adapter.TravelerListAdapter;
import com.rv.rvmine.bean.DeleteInfo;
import com.rv.rvmine.bean.DeleteVistorBean;
import com.rv.rvmine.bean.TravelerListBean;
import com.rv.rvmine.view.DeletePopupWindow;
import com.yuyife.okgo.OkGoUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 选择出游人
 */
@Route(path = Constance.ACTIVITY_URL_CHOOSEAVISITOR)
public class ChooseAVisitorActivity extends BaseStatusActivity<CommonPresenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;

    private TravelerListAdapter mAdapter;
    private final int TYPE_REQUEST_ADD = 105;
    private final int TYPE_REQUEST_EDIT = 2;
    private int mPage;
    private int countPage;
    private int visitorType;
    private int openType;
    private DeletePopupWindow deletePopupWindow;
    private List<TravelerListBean.DataBean> visitors = new ArrayList<>();
    private TravelerListBean.DataBean deletBean = null;

    private String hasChoIdCards; // 传过来的已经选择的身份证

    @Override
    protected int setLayout() {
        return R.layout.activity_choose_avisitor;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {

        visitorType = intent.getIntExtra("visitorType", -1);
        openType = intent.getIntExtra("openType", 0);
        hasChoIdCards = intent.getStringExtra("hasChoice");
        if (openType == 0) {
            titleView.setTitle("出游人");
        } else {
            titleView.setTitle("选择出游人");
        }
        titleView.setImageResource(R.id.iv_title_right, R.drawable.rv_common_icon_thestaff);
        titleView.setChildClickListener(R.id.iv_title_right, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(AddAVisitorActivity.getIntent(mActivity, null), TYPE_REQUEST_ADD);
            }
        });


        mAdapter = new TravelerListAdapter(openType);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (openType != -0) {
                    TravelerListBean.DataBean item = mAdapter.getItem(position);
                    if (item.getStatus()) {
                        Toast.makeText(getApplicationContext(), "不可重复添加", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Intent intent1 = getIntent();
                    intent1.putExtra("name", item.getName());
                    intent1.putExtra("phone", item.getPhone());
                    intent1.putExtra("idcard", item.getIdCard());
                    intent1.putExtra("id", item.getId());
                    setResult(RESULT_OK, intent1);
                    finish();
                }
            }
        });



        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_edit) {
                    startActivityForResult(AddAVisitorActivity.getIntent(mActivity, mAdapter.getItem(position)), TYPE_REQUEST_ADD);
                }
            }
        });
        mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                TravelerListBean.DataBean item = mAdapter.getItem(position);
                showDeletePopupWindow(item, view);
                return true;
            }
        });
    }

    private void showDeletePopupWindow(final TravelerListBean.DataBean item, View view) {
        if (deletePopupWindow == null) {
            deletePopupWindow = new DeletePopupWindow(this, new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    deleteDriver(item);
                    deletePopupWindow.dismiss();
                }
            });
            deletePopupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        }
        deletePopupWindow.showAsDropDown(view, view.getWidth() / 2 - deletePopupWindow.getContentView().getMeasuredWidth() / 2,
                -deletePopupWindow.getContentView().getHeight() / 2);

    }

//    @Override
//    public void finish() {
//        if (openType != 0) {
//            Intent intent = new Intent();
//            ArrayList<TravelerListBean.DataBean> selects;
////            if (selectBean == null) {
////                selectBean = mAdapter.getHasSelectBean();
////            }
////            if (selectBean != null) {
//                intent.putExtra("name", selectBean.getName());
//                intent.putExtra("phone", selectBean.getPhone());
//                intent.putExtra("idcard", selectBean.getIdCard());
//                intent.putExtra("id", selectBean.getId());
//                setResult(RESULT_OK, intent);
////            }
//
//        }
//        super.finish();
//    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        super.loadData(savedInstanceState, intent);
        getDrivingList();
    }

    @Override
    public void onShowResult(int requestType, BaseBean result) {
        if (requestType == 0) {
            processData((TravelerListBean) result);
        } else if (requestType == 1) {
            DeleteVistorBean bean = (DeleteVistorBean) result;
            if (bean != null) {
                if (bean.isRel()) {
                    if (deletBean != null) {
                        if (visitors.remove(deletBean)) {
                            mAdapter.notifyDataSetChanged();
                        }
                        deletBean = null;
                    }
                }
            }
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TYPE_REQUEST_ADD && resultCode == RESULT_OK) {
            getDrivingList();
        }
    }

    /**
     *
     */
    private void getDrivingList() {
        Map<String, Object> headMap = new LinkedHashMap<>();
        if (OkGoUtil.getToken() != null)
            headMap.put("Authorization", OkGoUtil.getToken());
        mPresenter.getDataHead(RvFrameConfig.VEHICLE_TOURUSER, 0, ApiConfig.HTTP_URL_GETTOURUSERLIST, TravelerListBean.class, headMap, true);
    }


    private void processData(TravelerListBean bean) {
        if (bean.getData() == null || bean.getData().size() == 0) {
            mAdapter.setEmptyView(getEmptyView(recyclerView, -1, "您还没有出游人信息哦"));
            return;
        }
        visitors.clear();
        if (visitorType == -1) {
            visitors.addAll(bean.getData());
        } else {
            for (TravelerListBean.DataBean b : bean.getData()) {
                if (visitorType == b.getIsChild().intValue()) {
                    if (hasChoIdCards.contains(b.getIdCard())) {
                        b.setStatus(true);
                    }
                    visitors.add(b);
                }
            }
        }
        if (visitors.isEmpty()) {
            mAdapter.setEmptyView(getEmptyView(recyclerView, -1, "您还没有出游人信息哦"));
        } else {
            mAdapter.setNewData(visitors);
        }
    }

    private void deleteDriver(TravelerListBean.DataBean item) {
        deletBean = item;
        Map<String, Object> headMap = new LinkedHashMap<>();
        if (OkGoUtil.getToken() != null)
            headMap.put("Authorization", OkGoUtil.getToken());
        mPresenter.postBodyData(1, ApiConfig.HTTP_URL_DELETEDRIVER, DeleteVistorBean.class, new DeleteInfo(deletBean.getId()), headMap, true);
    }

}
