package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.util.date.DateUtils;
import com.tofly.ghrq_route.common.util.file.FileSizeUtil;
import com.tofly.ghrq_route.common.util.file.IntentBuilder;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zombiexq on 2016/7/6.
 */
    public class LogFilesAdapter extends ARecyclerBaseAdapter<LogFilesAdapter.MyViewHolder, String> {

    private Context mContext;
    private List<String> mLoglist;
    private Set<Integer> selectedPostions = new HashSet<>();
    private OnItemClickListener mItemClickListener;


    public LogFilesAdapter(Context context, List<String> logList) {
        super(context, logList);
        this.mContext = context;
        this.mLoglist = logList;
    }

    public void setData(List<String> data){
        mLoglist = data;
        notifyDataSetChanged();
    }

    public Set<Integer> getSelectedPositionSet() {
        return selectedPostions;
    }

    public List<String> getSelectFiles(){
        List<String> files = new ArrayList<>();
        if (selectedPostions.size() > 0){
            for (int pos : selectedPostions){
                String file = getItem(pos);
                files.add(file);
            }
        }
        return files;
    }

    public void toggleSelection(int position){
        if (selectedPostions.contains(position)){
            selectedPostions.remove(position);
        }else {
            selectedPostions.add(position);
        }
        notifyItemChanged(position);
    }

    public void allSelected() {
        selectedPostions.clear();
        for (int i = 0; i < mLoglist.size(); i++)
            selectedPostions.add(i);
        this.notifyDataSetChanged();
    }

    public void cancelSelected() {
        selectedPostions.clear();
        this.notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext)
                .inflate(R.layout.listview_log_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, final int position) {
        final String filePath = mLoglist.get(position);
        if (filePath != null){
            File file = new File(filePath);
            if (file != null){
                /*文件名*/
                String fileName = file.getName();
                viewHolder.textLogName.setText(fileName);

                /*修改日期*/
                String date = DateUtils.getHMS(file.lastModified());
                viewHolder.textLogDate.setText(date);

                /*文件大小*/
                try {
                    long size = FileSizeUtil.getFileSize(file);
                    String fileSize = FileSizeUtil.FormetFileSize(size);
                    if (fileSize != null)
                        viewHolder.textLogSize.setText(fileSize);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                viewHolder.textLogName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntentBuilder.viewFile(mContext, filePath);
                    }
                });
            }

            viewHolder.checkBoxLog.setVisibility(View.VISIBLE);
            boolean iscChecked = selectedPostions.contains(position);
            viewHolder.checkBoxLog.setChecked(iscChecked);

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   toggleSelection(position);
                }
            });

            viewHolder.checkBoxLog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toggleSelection(position);
                }
            });
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_log_name)
        TextView textLogName;
        @BindView(R.id.text_log_date)
        TextView textLogDate;
        @BindView(R.id.text_log_size)
        TextView textLogSize;
        @BindView(R.id.checkbox_log)
        CheckBox checkBoxLog;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
