package com.handset.serialportsensor.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.handset.serialportsensor.R;
import com.handset.serialportsensor.utils.Slog;
import com.handset.serialportsensor.view.LinearLayoutManagerCus;

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

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;

/**
 * Created by zy on 2019/2/27.
 */


public class ViewOperateLogListActivity extends AppCompatActivity {
    RecyclerView mRecyclerView;
    MyAdapter myAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_operate_log);
        mRecyclerView = findViewById(R.id.recycle_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManagerCus(this));
        DefaultItemAnimator animator = new DefaultItemAnimator();
        animator.setRemoveDuration(1000);
        mRecyclerView.setItemAnimator(animator);
        TextView tvTitle = findViewById(R.id.title_text);
        ImageView ivBack = findViewById(R.id.title_back);
        tvTitle.setText("操作日志");
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewOperateLogListActivity.this.finish();
            }
        });
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, VERTICAL));
        getOperateLog();
    }

    private void getOperateLog() {
        List<File> list = Slog.getOperateLogFiles();
        //设置适配器
        if (myAdapter == null) {
            myAdapter = new MyAdapter(this, list);
            mRecyclerView.setAdapter(myAdapter);
        } else {
            myAdapter.setDataList(list);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvIndex;
        TextView tvLog;
        ConstraintLayout cl_content;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvIndex = itemView.findViewById(R.id.tv_item_index);
            tvLog = itemView.findViewById(R.id.tv_name);
            cl_content = itemView.findViewById(R.id.cl_content);
        }
    }

    static class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
        List<File> mDataList;

        private OnItemClickListener mOnItemClickListener;

        Context mContext;

        public MyAdapter(Context context, List<File> mList) {
            mContext = context;
            this.mDataList = mList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.operate_log_item, null, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
            final File file = mDataList.get(i);
            viewHolder.tvIndex.setText((i + 1) + "");
            viewHolder.tvLog.setText(file.getName());
            viewHolder.cl_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ViewLogActivity.class);
                    intent.putExtra("file_path", file.getAbsolutePath());
                    mContext.startActivity(intent);
                }
            });
        }

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

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setDataList(List<File> dataList) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }

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