package com.icod.yk_printer_test.adapter;

import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.icod.yk_printer_test.R;
import com.icod.yk_printer_test.model.BluetoothBean;
import com.icod.yk_printer_test.model.BluetoothInfo;

import java.util.LinkedList;
import java.util.Map;

/**
 * Created by crf on 2018/9/6.
 */
public class BluetoothAdapter extends BaseExpandableListAdapter {

    private BluetoothBean mBluetoothBean;
    private Context mContext;
    public BluetoothAdapter(Context context, BluetoothBean bluetoothBean){
        mBluetoothBean=bluetoothBean;
        mContext=context;

    }
    @Override
    public int getGroupCount() {
        String[] group = mBluetoothBean.getGroup();
        if (group!=null){
            return group.length;
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        Map<Integer, LinkedList<BluetoothInfo>> child = mBluetoothBean.getChild();
        if(child!=null){
        LinkedList<BluetoothInfo> bluetoothInfos = child.get(groupPosition);
        if (bluetoothInfos!=null){
           return bluetoothInfos.size();
        }}
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        String[] group = mBluetoothBean.getGroup();
        if (group!=null){
            return group[groupPosition];
        }
        return "";
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        LinkedList<BluetoothInfo> bluetoothInfos = mBluetoothBean.getChild().get(groupPosition);
        if (bluetoothInfos!=null){
            return bluetoothInfos.get(childPosition);
        }
        return "";
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bluetooth_adapter_group, null);
            groupViewHolder=new GroupViewHolder();
            groupViewHolder.groupName= (TextView) convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(groupViewHolder);
        }else{
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
            groupViewHolder.groupName.setText(mBluetoothBean.getGroup()[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder groupViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bluetooth_adapter_child_item,
                    null);
            groupViewHolder=new ChildViewHolder();
            groupViewHolder.childName= (TextView) convertView.findViewById(R.id.tv_bluetooth_name);
            groupViewHolder.ivSelect= (ImageView) convertView.findViewById(R.id.iv_select);
            groupViewHolder.ivIcod= (ImageView) convertView.findViewById(R.id.iv_icod);

            groupViewHolder.tvAddress= (TextView) convertView.findViewById(R.id.tv_bluetooth_address);
            convertView.setTag(groupViewHolder);
        }else{
            groupViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if (groupPosition==0){
            groupViewHolder.ivSelect.setVisibility(View.VISIBLE);
        }else{
            groupViewHolder.ivSelect.setVisibility(View.GONE);
        }
        LinkedList<BluetoothInfo> bluetoothInfos = mBluetoothBean.getChild().get(groupPosition);
        if (bluetoothInfos.size()>0) {
          BluetoothInfo info=  bluetoothInfos.get(childPosition);
            if (info != null) {
                groupViewHolder.childName.setText(info.getName() + " ");
                groupViewHolder.tvAddress.setText(info.getAddress());
                groupViewHolder.ivSelect.setImageResource(info.isConnected() ? R.drawable
                        .selected : R.drawable.no_select);

                if (info.getType()== BluetoothClass.Device.Major.COMPUTER){
                    groupViewHolder.ivIcod.setImageResource(R.drawable.bluetooth_computer);
                }else if (info.getType()== BluetoothClass.Device.Major.AUDIO_VIDEO){
                    groupViewHolder.ivIcod.setImageResource(R.drawable.bluetooth_video);
                }else if (info.getType()== BluetoothClass.Device.Major.MISC){
                    groupViewHolder.ivIcod.setImageResource(R.drawable.bluetooth_misc);
                }else if (info.getType()== BluetoothClass.Device.Major.PHONE){
                    groupViewHolder.ivIcod.setImageResource(R.drawable.bluetooth_phone);
                }else if (info.getType()== BluetoothClass.Device.Major.WEARABLE){
                    groupViewHolder.ivIcod.setImageResource(R.drawable.bluetooth_wearable);
                }else  {
                    groupViewHolder.ivIcod.setImageResource(R.drawable.bluetooth_icod);
                }
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    static class GroupViewHolder{
        public TextView groupName;
    }
    static class ChildViewHolder{
        public TextView childName;
        public ImageView ivSelect;
        public ImageView ivIcod;
        public TextView tvAddress;

    }
}
