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.QuestionKindEntity;
import com.zhilin.patrol.tool.DensityUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewProvider;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 问题类型
 */
public class QuestionTypeActivity extends BaseActivity {

    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;
    private MultiTypeAdapter mAdapter;
    /** 点击位置 */
    private int mPos = -1;

    private ArrayList mQuestionInfoList;

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

    }

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

    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());

        mAdapter = new MultiTypeAdapter(mQuestionInfoList);
        mAdapter.applyGlobalMultiTypePool();
        mAdapter.register(QuestionKindEntity.InfoListBean.class, new ItemTitle());
        mAdapter.register(QuestionKindEntity.ListBean.class, new ItemContent());
        mRecyclerView.setAdapter(mAdapter);
    }

    /** 标题布局 */
    class ItemTitle extends ItemViewProvider<QuestionKindEntity.InfoListBean, ItemTitle.ViewHolder> {

        @NonNull
        @Override
        protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
            View itemView = inflater.inflate(R.layout.item_question_type_title, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull QuestionKindEntity.InfoListBean bean) {
            holder.titleTv.setText(bean.getTitle());
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView titleTv;

            public ViewHolder(View itemView) {
                super(itemView);
                titleTv = itemView.findViewById(R.id.tv_question_title);
            }
        }
    }

    /** 内容布局 */
    class ItemContent extends ItemViewProvider<QuestionKindEntity.ListBean, ItemContent.ViewHolder> {

        @NonNull
        @Override
        protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
            View itemView = inflater.inflate(R.layout.item_question_type, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final QuestionKindEntity.ListBean bean) {
            holder.questionTv.setText(bean.getTitle());
            List<QuestionKindEntity.Bean> sonCell = bean.getSonCell();
            int count = 0;
            for (QuestionKindEntity.Bean b : sonCell) {
                if (b.isSelected()) {
                    count++;
                }
            }
            if (count != 0) {
                holder.numTv.setVisibility(View.VISIBLE);
                holder.numTv.setText(count + "");
            } else {
                holder.numTv.setVisibility(View.GONE);
                holder.numTv.setText("");
            }
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPos = holder.getAdapterPosition();
                    Intent intent = new Intent(QuestionTypeActivity.this, QuestionTypeSelectActivity.class);
                    intent.putExtra("ListBean", bean);
                    startActivityForResult(intent, 1);
                }
            });
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView questionTv;
            TextView numTv;
            View view;

            public ViewHolder(View itemView) {
                super(itemView);
                view = itemView;
                questionTv = itemView.findViewById(R.id.tv_question);
                numTv = itemView.findViewById(R.id.tv_num);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1 && data != null) {
            if (mPos == -1) return;
            if (mQuestionInfoList != null) {
                for (Object o : mQuestionInfoList) {
                    if (o == null || !(o instanceof QuestionKindEntity.ListBean)) continue;
                    QuestionKindEntity.ListBean listBean = (QuestionKindEntity.ListBean) o;
                    if (listBean.getSonCell() == null) continue;
                    for (QuestionKindEntity.Bean b : listBean.getSonCell()) {
                        b.setSelected(false);
                    }
                }
            }
            QuestionKindEntity.ListBean listBean = (QuestionKindEntity.ListBean) data.getSerializableExtra("ListBean");
            mQuestionInfoList.set(mPos, listBean);
//            mAdapter.notifyDataSetChanged();
            finish();
        }
    }

}
