package com.sheep.gamegroup.view.activity;

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

import com.bumptech.glide.Glide;
import com.sheep.gamegroup.util.FileUtil;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.PngUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
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.io.File;
import java.io.FileFilter;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;

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


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

    @BindView(R.id.user_label_commit_tv)
    TextView user_label_commit_tv;

    @Override
    public void initView() {
        TitleBarUtils
                .getInstance()
                .setTitle(this, "Sheep目录png测试")
                .setTitleFinish(this);
        user_label_commit_tv.setVisibility(View.GONE);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        loadData(getList());
    }

    private List<File> getList() {
        File dir = new File(DIR);
        File[] files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return TextUtils.equals(FileUtil.getExtensionName(file.getName()), "png");
            }
        });
        return ListUtil.asList(files);
    }
    private void loadData(List<File> list) {
        user_label_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        user_label_list.setAdapter(new AdbCommonRecycler<File>(SheepApp.getInstance(), list){

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

            @Override
            public void convert(ViewHolder holder, final File item) {
                ImageView app_info_iv = holder.itemView.findViewById(R.id.app_info_iv);
                TextView app_info_name_tv = holder.itemView.findViewById(R.id.app_info_name_tv);
                TextView app_info_package_name_tv = holder.itemView.findViewById(R.id.app_info_package_name_tv);
                TextView app_info_size_tv = holder.itemView.findViewById(R.id.app_info_size_tv);
                TextView app_info_path_tv = holder.itemView.findViewById(R.id.app_info_path_tv);

                Glide.with(SheepApp.getInstance())
                        .load(item)
                        .into(app_info_iv);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            onClickItem(item);
                        } catch (Exception e) {
                            e.printStackTrace();
                            G.showToast(e.getMessage());
                        }
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        return true;
                    }
                });
            }
        });

    }

    private void onClickItem(final File file) throws Exception {
        //加密文本内容：包名;签名md5;试玩应用包名;试玩应用签名md5;任务id;图片总数;图片位置
        String iEndData = PngUtil.decodePng(file.getAbsolutePath(), "com.realicing.android.upgrade.sheep", "280;7;0");
        if(iEndData == null){
            G.showToast("失败");
        } else if(TextUtils.isEmpty(iEndData)){
            G.showToast("添加iEnd data成功");
        } else {
            G.showToast(iEndData);
        }
    }

    @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() {
    }

}
