package com.mjsheng.myappstore.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.bean.LocalAppInfo;
import com.mjsheng.myappstore.dialog.GlobalAlertDialog;
import com.mjsheng.myappstore.image.ImageLoader;
import com.mjsheng.myappstore.utils.ApkUtils;
import com.mjsheng.myappstore.utils.SaveListUtils;
import com.mjsheng.myappstore.utils.Utils;
import com.mjsheng.myappstore.widget.Sound;

import java.util.ArrayList;
import java.util.List;

public class UninstallListAdapter extends RecyclerView.Adapter<UninstallListAdapter.UninstallViewHolder> {

    private Context mContext;
    public List<LocalAppInfo> mDataList = new ArrayList<>();
    private Sound sound;
    private setItemSizeListener mListener;
    private final List<String> mForceInstallList;

    public UninstallListAdapter(Context context, setItemSizeListener listener) {
        mContext = context;
        mListener = listener;
        sound = new Sound(context);
        mForceInstallList = SaveListUtils.getList();
    }


    public void loadmore(List<LocalAppInfo> mList) {
        int number = mDataList.size();
        mDataList.addAll(mList);
        notifyItemRangeInserted(number, mDataList.size());
    }

    @Override
    public UninstallViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View v = View.inflate(parent.getContext(), R.layout.uninstall_list_item, null);
        v.setLayoutParams(layoutParams);
        return new UninstallViewHolder(v);
    }

    @Override
    public void onBindViewHolder(UninstallViewHolder holder, int position) {
        LocalAppInfo info = mDataList.get(position);
        if (info != null) {
            String iconUrl = info.getIconUrl();
            if (!TextUtils.isEmpty(iconUrl)) {
                ImageLoader.getDefault().loadUrl(mContext, iconUrl, holder.appIcon);
            } else {
                holder.appIcon.setImageDrawable(info.getIcon());
            }
            holder.title.setText(info.getAppName());
            holder.appVersion.setText("v" + info.getVersion());

            String totalLength = Formatter.formatFileSize(mContext, info.getSize());
            holder.appSize.setText(totalLength);

            holder.openLayout.setOnClickListener(new OnButtonClickListener(info
                    .getPackageName(), info.getAppName()));
            holder.uninstallLayout
                    .setOnClickListener(new OnButtonClickListener(info
                            .getPackageName(), info.getAppName()));
            if (mForceInstallList == null || mForceInstallList.isEmpty()) {
                return;
            }
            if (mForceInstallList.contains(info.getPackageName())) {
                holder.uninstallLayout.setVisibility(View.INVISIBLE);
            } else {
                holder.uninstallLayout.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setDatas(List<LocalAppInfo> dataList) {
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void addDatas(LocalAppInfo info) {
        mDataList.add(info);
        mListener.setItemSize(mDataList.size());
        notifyItemInserted(mDataList.size() - 1);
    }

    public void notifyItem(int position, LocalAppInfo info) {
        mDataList.set(position, info);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mDataList.size());
        mListener.setItemSize(mDataList.size());
    }

    public void removeItem(String packageName) {
        int position = -1;
        for (int i = 0; i < mDataList.size(); i++) {
            if (packageName.equals(mDataList.get(i).getPackageName())) {
                position = i;
            }
        }
        if (position != -1) {
            mDataList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, mDataList.size());
            mListener.setItemSize(mDataList.size());
        }
    }

    private void showUninstallAlert(final String packageName, final String AppName) {
        GlobalAlertDialog alert = new GlobalAlertDialog.Builder(mContext)
                .setCancelable(true)
                .setTitle(AppName)
                .setPositiveButton("卸\t载", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        //                        Utils.unInstallAPP(mContext, packageName);
                        ApkUtils.deleteApkInSilence(packageName);
                    }
                })
                .setNegativeButton("取\t消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                })
                .create(R.layout.uninstall_alert_dialog);
        alert.show();
    }

    public interface setItemSizeListener {
        void setItemSize(int size);
    }

    public class UninstallViewHolder extends RecyclerView.ViewHolder {
        private ImageView appIcon;
        private TextView title;
        private TextView appVersion;
        private TextView appSize;
        private LinearLayout appBg, openLayout, uninstallLayout;

        public UninstallViewHolder(View itemView) {
            super(itemView);
            appBg = (LinearLayout) itemView.findViewById(R.id.app_bg);
            appIcon = (ImageView) itemView
                    .findViewById(R.id.app_icon);
            title = (TextView) itemView.findViewById(R.id.title);
            appVersion = (TextView) itemView
                    .findViewById(R.id.app_version);
            appSize = (TextView) itemView
                    .findViewById(R.id.app_size);
            openLayout = (LinearLayout) itemView
                    .findViewById(R.id.llDownload);
            uninstallLayout = (LinearLayout) itemView
                    .findViewById(R.id.llCancle);
        }
    }

    private class OnButtonClickListener implements View.OnClickListener {
        private String packageName, AppName;

        public OnButtonClickListener(String packageName, String AppName) {
            this.packageName = packageName;
            this.AppName = AppName;
        }

        @Override
        public void onClick(View v) {
            sound.playSoundEffect();
            switch (v.getId()) {
                case R.id.llDownload:
                    Utils.startApp(mContext, packageName, "");
                    break;
                case R.id.llCancle:
                    showUninstallAlert(packageName, AppName);
                    break;
            }
        }
    }

}