package com.okq.C24detector.Adapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.kydiwen.easyserial.Utils.DensityUtil;
import com.okq.C24detector.Bean.SampleBean;
import com.okq.C24detector.Constant.SampleConstant;
import com.okq.C24detector.Db.C24Openhelper;
import com.okq.C24detector.Db.DatabaseUtils;
import com.okq.C24detector.R;
import com.okq.C24detector.Utils.toastUtils;
import com.okq.C24detector.Widget.NobgDialog;

import java.util.ArrayList;

/**
 * Created by 孙文权 on 2016/12/5.
 * 样品列表数据适配器
 */

public class SamplesAdapter
        extends BaseAdapter
{
    private Context context;
    private ArrayList<SampleBean> data;
    private SQLiteDatabase database;

    public SamplesAdapter(Context context, ArrayList<SampleBean> data, SQLiteDatabase database){
        this.context = context;
        this.data = data;
        this.database = database;
    }

    @Override
    public int getCount(){
        return data.size();
    }

    @Override
    public Object getItem(int position){
        return data.get(position);
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context,
                                       R.layout.layout_sample_item,
                                       null);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.masculine = (EditText) convertView.findViewById(R.id.masculine);
            holder.delete = (TextView) convertView.findViewById(R.id.delete);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(data.get(position)
                                .getName());
        holder.masculine.setText(data.get(position)
                                     .getMasculine());
        if(Integer.parseInt(data.get(position)
                                .getType()) == SampleConstant.SampleUseradd){
            holder.masculine.setBackgroundResource(R.drawable.edit_bg_selector);
        }else{
            holder.masculine.setBackgroundDrawable(null);
        }
        //根据样品类型设置是否可以删除
        holder.delete.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if(Integer.parseInt(data.get(position)
                                        .getType()) == SampleConstant.SampleDefault){
                    toastUtils.show(context,
                                    context.getString(R.string.default_not_delete));
                }else{
                    DatabaseUtils.deleteSample(database,
                                               data.get(position)
                                                   .getName());
                    data.remove(position);
                    notifyDataSetChanged();
                }
            }
        });
        //设置假阳性参数点击事件
        holder.masculine.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //如果是系统默认参数提示无法修改，如果是用户添加数据允许修改
                if(Integer.parseInt(data.get(position)
                                        .getType()) == SampleConstant.SampleDefault){
                    toastUtils.show(context,
                                    context.getResources()
                                           .getString(R.string.default_not_edit));
                }else{
                    View view = View.inflate(context,
                                             R.layout.dialog_input_masculine,
                                             null);
                    final EditText input = (EditText) view.findViewById(R.id.input);
                    TextView ensure = (TextView) view.findViewById(R.id.ensure);
                    TextView cancel = (TextView) view.findViewById(R.id.cancel);
                    final NobgDialog dialog = new NobgDialog(context);
                    dialog.setContentView(view);
                    dialog.show();
                    WindowManager.LayoutParams params = dialog.getWindow()
                                                              .getAttributes();
                    params.width = DensityUtil.dip2px(context,
                                                      400);
                    params.height = DensityUtil.dip2px(context,
                                                       300);
                    params.gravity = Gravity.CENTER;
                    dialog.getWindow()
                          .setAttributes(params);
                    ensure.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){
                            if(! TextUtils.isEmpty(input.getText()
                                                        .toString())){
                                //修改数据库数据
                                ContentValues values = new ContentValues();
                                values.put(SampleConstant.SampleMasculine,
                                           input.getText()
                                                .toString());
                                DatabaseUtils.updateSample(C24Openhelper.getInstance(context)
                                                                        .getWritableDatabase(),
                                                           values,
                                                           data.get(position)
                                                               .getName());
                                //更新列表
                                data.get(position)
                                    .setMasculine(input.getText()
                                                       .toString());
                                notifyDataSetChanged();
                                dialog.dismiss();
                            }else{
                                toastUtils.show(context,
                                                context.getString(R.string.notice_input_efficacious));
                            }
                        }
                    });
                    cancel.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){
                            dialog.dismiss();
                        }
                    });
                }
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView name;
        EditText masculine;
        TextView delete;
    }
}
