package com.sheep.gamegroup.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.android.view.tag.FlowLayout;
import com.kfzs.android.view.tag.TagAdapter;
import com.kfzs.android.view.tag.TagFlowLayout;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserLabel;
import com.sheep.gamegroup.model.entity.UserLabelList;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/6/8.
 * realicing@sina.com
 */
public class ActUserLabelList extends BaseActivity {
    @Override
    protected int getLayoutId() {
        return R.layout.act_user_label_list;
    }


    @BindView(R.id.user_label_list)
    RecyclerView user_label_list;

    @Override
    public void initView() {
        TitleBarUtils
                .getInstance()
                .setTitle(this, "请选择您喜欢的标签")
                .setTitleListen(this, -2,null);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        SheepApp.getInstance().getNetComponent().getApiService().getLabelList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<UserLabelList> checkLabelList = baseMessage.getDatas(UserLabelList.class);
                        if (!ListUtil.isEmpty(checkLabelList)) {
                            loadData(checkLabelList);
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast(baseMessage);
                    }
                });
    }
    private List<UserLabelList> checkLabelList;
    private void loadData(List<UserLabelList> checkLabelList) {
        this.checkLabelList = checkLabelList;
        user_label_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        user_label_list.setAdapter(new AdbCommonRecycler<UserLabelList>(SheepApp.getInstance(), checkLabelList){

            @Override
            public int getViewIdByType(int type) {
                return R.layout.user_label_item;
            }

            @Override
            public void convert(ViewHolder holder, final UserLabelList userLabelList) {
                View rootConvertView = holder.itemView;
                TextView user_label_tv = rootConvertView.findViewById(R.id.user_label_tv);
                ViewUtil.setText(user_label_tv, userLabelList.getLabel_type_name());

                TagFlowLayout tagList = rootConvertView.findViewById(R.id.user_label_tagflow_layout);
                if(tagList != null) {
                    TagAdapter adapter = new TagAdapter<UserLabel>(userLabelList.getLabels()) {
                        @Override
                        public View getView(FlowLayout parent, int position, UserLabel userLabel) {

                            TextView tv = (TextView) LayoutInflater.from(ActUserLabelList.this).inflate(R.layout.user_label, parent, false);
                            ViewUtil.setText(tv, userLabel.getLabel_name());
                            return tv;
                        }
                    };

                    tagList.setAdapter(adapter);
                    tagList.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                        @Override
                        public boolean onTagClick(View view, int position, FlowLayout parent) {
                            UserLabel userLabel = ListUtil.getItem(userLabelList.getLabels(), position);
                            if(userLabel != null) {
                                userLabel.setChecked(!userLabel.isChecked());
                                return true;
                            }
                            return false;
                        }
                    });
                }
            }
        });

    }
    @OnClick({R.id.user_label_commit_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_label_commit_tv:
                toCommit();
                break;
        }
    }

    private void toCommit() {
        if(ListUtil.isEmpty(checkLabelList)){
            G.showToast(R.string.loading_data);
            return;
        }
        List<Integer> list = ListUtil.emptyList();
        for (UserLabelList userLabelList : checkLabelList) {
            for (UserLabel userLabel : userLabelList.getLabels()) {
                if(userLabel.isChecked()) {
                    list.add(userLabel.getLabel_id());
                }
            }
        }
        LogUtil.println("用户标签："+list.size());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("label_ids", list);
        SheepApp.getInstance().getNetComponent().getApiService().postLabelList(jsonObject)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                Jump2View.getInstance().checkCommendApp(ActUserLabelList.this, null);
                                finish();
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                G.showToast(baseMessage);
                                if(BuildConfig.DEBUG){
                                    Jump2View.getInstance().checkCommendApp(ActUserLabelList.this, null);
                                    finish();
                                }
                            }
                        });
    }

}
