package com.sheep.gamegroup.view.activity;

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

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.AppInfo;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.Md5Util;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
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.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by realicing on 2018/6/19.
 * realicing@sina.com
 */
public class ActInstallApkList 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, "已经安装的应用列表")
                .setTitleFinish(this);
        user_label_commit_tv.setVisibility(View.GONE);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        loadData(SysAppUtil.getAppInfos());
    }
    private void loadData(List<AppInfo> appInfoList) {
        user_label_list.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        user_label_list.setAdapter(new AdbCommonRecycler<AppInfo>(SheepApp.getInstance(), appInfoList){

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

            @Override
            public void convert(ViewHolder holder, final AppInfo appInfo) {
                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);

                app_info_iv.setImageDrawable(appInfo.getIcon());
                ViewUtil.setText(app_info_name_tv, appInfo.getAppName());
                app_info_name_tv.append(Md5Util.getFileMD5(new File(appInfo.getSourceDir())));
                ViewUtil.setText(app_info_package_name_tv, appInfo.getPackageName());
                ViewUtil.setText(app_info_size_tv, appInfo.getApkSize());
                ViewUtil.setText(app_info_path_tv, appInfo.getSourceDir()+"\n"+appInfo.getSha1());
                app_info_path_tv.append("\n"+ (appInfo.isUserApp() ? "用户app":"系统app")  +"\u3000"+ (appInfo.isSD() ? "SD卡" : "手机内存"));

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onClickItem(view, appInfo);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        StringUtils.CopyText(appInfo.getSha1());
                        G.showToast("复制sha1成功："+appInfo.getSha1());
                        return true;
                    }
                });
            }
        });

    }

    private void onClickItem(View view, final AppInfo appInfo) {
        StringUtils.CopyText(appInfo.getPackageName());
        readApp(appInfo);
    }


    private void readApp(AppInfo appInfo) {
        String sourceDir = appInfo.getSourceDir();
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.contains("../")) {
                    break;
                }
                LogUtil.println("readApp", appInfo.getAppName(), appInfo.getPackageName(), entryName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @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() {
    }

}
