package com.zhilin.patrol.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.zhilin.patrol.R;
import com.zhilin.patrol.bean.LoginEntity;
import com.zhilin.patrol.bean.QuestionKindEntity;
import com.zhilin.patrol.http.HttpUrl;
import com.zhilin.patrol.tool.DensityUtil;
import com.zhilin.patrol.tool.DialogUtil;
import com.zhilin.patrol.tool.IpUtils;
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 me.drakeet.multitype.ItemViewProvider;
import me.drakeet.multitype.MultiTypeAdapter;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 选择问题类型
 */
public class QuestionTypeSelectActivity extends BaseActivity {

    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;
    private QuestionKindEntity.ListBean mBean;
    private CommonAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_type);
        ButterKnife.bind(this);
        mBean = (QuestionKindEntity.ListBean) getIntent().getSerializableExtra("ListBean");
        setTitle("问题类型");
//        setRightTv("确定");
//        setRightClick(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.putExtra("ListBean",mBean);
//                setResult(1,intent);
//                finish();
//            }
//        });
        init();
    }

    private void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(getResources().getColor(R.color.line_gray))
                .size(DensityUtil.dip2px(this, 1))
                .build());
        final List<QuestionKindEntity.Bean> sonCell = mBean.getSonCell();
        mAdapter = new CommonAdapter<QuestionKindEntity.Bean>(this, R.layout.item_question_type_select, sonCell) {
            @Override
            protected void convert(ViewHolder holder, final QuestionKindEntity.Bean bean, int position) {
                holder.setText(R.id.tv_question, bean.getqName());
                holder.setVisible(R.id.iv_select, bean.isSelected());
                if (bean.isSelected()) {
                    holder.setTextColorRes(R.id.tv_question, R.color.text_orange);
                } else {
                    holder.setTextColorRes(R.id.tv_question, R.color.text_black);
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (QuestionKindEntity.Bean b : sonCell) {
                            b.setSelected(false);
                        }
                        bean.setSelected(true);
//                        mAdapter.notifyDataSetChanged();
                        finish();
                    }
                });
            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        intent.putExtra("ListBean",mBean);
        setResult(1,intent);
        super.finish();
    }
}
