package com.sheep.gamegroup.view.activity;

import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sheep.gamegroup.absBase.AbsChooseImageActivity;
import com.sheep.gamegroup.model.entity.InputAndUrlList;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.KeyEventUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.io.File;

import butterknife.BindView;

/**
 * Created by realicing on 2018/11/12.
 * realicing@sina.com
 * 输入自定义字段
 */
public class ActInputAndPickerImg extends AbsChooseImageActivity {
//
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(getLayoutId());
//        ButterKnife.bind(this);
//        initView();
//        initData();
//    }

    protected int getLayoutId() {
        return R.layout.act_input_and_piker_img;
    }

    @BindView(R.id.input_and_picker_img_label)
    TextView input_and_picker_img_label;
    @BindView(R.id.input_and_picker_img_et)
    AppCompatEditText input_and_picker_img_et;
    @BindView(R.id.input_and_picker_img_rv)
    RecyclerView recyclerView;

    private InputAndUrlList inputAndUrlList = new InputAndUrlList();

    public void initView() {
        inputAndUrlList.getList().add(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_image_view, inputAndUrlList.getList()) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                ImageView imageView = helper.getView(R.id.img_detail);
                if (item == null) {
                    ViewUtil.setImage(imageView, ViewUtil.getNetImgByName("add_img"));
                    helper.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int maxCount = DEFAULT_MAX_COUNT - inputAndUrlList.getList().size();
                            if (inputAndUrlList.getList().contains(null))
                                maxCount++;
                            showChooseDialog(false, false, maxCount);
                        }
                    });
                } else {
                    Glide.with(SheepApp.getInstance())
                            .load(new File(item))
                            .into(imageView);
                    helper.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                        }
                    });
                }
            }
        });
    }

    private String label;

    public void initData() {
        label = DataUtil.getObject(getIntent(), String.class);
        ViewUtil.setText(input_and_picker_img_label, getString(R.string.input_x, label));
    }

    //点击确定按钮
    public void onClickCommit(View view) {
        String input = input_and_picker_img_et.getText().toString();
        if (TextUtils.isEmpty(input)) {
            G.showToast(getString(R.string.x_cant_is_null, label));
            return;
        }
        if (inputAndUrlList.getList().isEmpty()) {
            G.showToast(R.string.need_picker_img);
            return;
        }
        inputAndUrlList.setInput(input);
        inputAndUrlList.getList().remove(null);
        DataUtil.getInstance().putData(InputAndUrlList.class.getSimpleName(), inputAndUrlList);
        setResult(RESULT_OK);
        finish();
    }

    //点击空白区域
    public void onClickNullView(View view) {
        KeyEventUtil.sendKeyDownUp(KeyEvent.KEYCODE_BACK);
    }

    @Override
    protected void onNotGetImage(String msg) {
        G.showToast(msg);
    }

    @Override
    protected void onGetImage(String path) {
        inputAndUrlList.getList().remove(null);
        for (String photo : photos) {
            if (!inputAndUrlList.getList().contains(photo)) {
                inputAndUrlList.getList().add(photo);
            }
        }
        if (inputAndUrlList.getList().size() < DEFAULT_MAX_COUNT)
            inputAndUrlList.getList().add(null);
        ViewUtil.notifyDataSetChanged(recyclerView);
    }

    @Override
    protected void onSetNetImageUrl(String url) {
        dismissLoaddingDialog();
    }

    @Override
    protected void onGetNetImageUrl(String url) {

    }
}
