package com.bonait.bnframework.modules.home.fragment.from;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.helper.AES;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.common.view.MyLayoutManager;
import com.bonait.bnframework.modules.home.adapter.image_sp_adapter;
import com.bonait.bnframework.modules.home.adapter.sp_adapter;
import com.bonait.bnframework.modules.home.fragment.mode.image_sp;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ImageChooseActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar mTopBar;//顶部标题
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;//image列表
    private static final String CACHE_PATH= Environment.getExternalStorageDirectory().getAbsolutePath()+"/hblxiaochaodb/WebImage";
    List<image_sp> data =new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_choose);
        ButterKnife.bind(this);
        initTopBar();
        initData();
    }

    private void initTopBar() {
        mTopBar.setTitle("选择图片");
        mTopBar.addLeftImageButton(R.mipmap.fanhui,R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initData() {
        try{
            File path = new File(CACHE_PATH);// 获得路径
            File[] files = path.listFiles();// 读取文件
            data.clear();
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                image_sp s=new image_sp();
                s.url=fileName;
                data.add(s);
            }
            MyLayoutManager layout = new MyLayoutManager();
            layout.setAutoMeasureEnabled(true);
            recyclerView.setLayoutManager(layout);
            image_sp_adapter spadapter = new image_sp_adapter(this,data);
            recyclerView.setAdapter(spadapter);

            spadapter.mListener=new MyClickListener() {
                @Override
                public void clickListener(View v, Object data) {
                    image_sp sp=(image_sp)data;
                    MessageManager.getInstance().sendMessage(MessageName.ClickImage,sp.url);
                    finish();
                }
                @Override
                public void clickListenerNew(View v, int k, Object data) {
                }
            };
        }catch(Exception e){
            ToastUtils.info("异常信息:"+e.getMessage());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected boolean canDragBack() {
        return false;
    }
}