package com.example.lenovo.addressbook;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Person> data;
    private AccessoriesAdapter mAdapter;
    private Person person;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化数据
        Contacts contacts = new Contacts();
        data = contacts.contactDb;

        //为布局指定数据适配器
        mAdapter = new AccessoriesAdapter();
        listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showMessage("你选中了:" + data.get(position).getName());
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                ArrayList<String> person = new ArrayList<String>();
                person.add(data.get(position).get(Person.Field.NAME));
                person.add(data.get(position).get(Person.Field.PHONE));
                person.add(data.get(position).get(Person.Field.EMAIL));
                person.add(data.get(position).get(Person.Field.WEIXIN));
                person.add(data.get(position).get(Person.Field.QQ));
                person.add(data.get(position).get(Person.Field.ORGANIZATION));
                person.add(data.get(position).get(Person.Field.ADDRESS));
                person.add(data.get(position).get(Person.Field.COLOR));
                intent.putStringArrayListExtra("Person", person);
                startActivity(intent);
            }
        });
    }
    public ListView getListView() {
        return listView;
    }
    private void sms(String telNumber) {
        Uri sms_uri = Uri.parse("smsto:+"+telNumber);
    Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
    startActivity(sms_intent);
    }
    private void makeCall(String telNumber) {
        Uri call_uri = Uri.parse("tel:" + telNumber);
        Intent intent = new Intent(Intent.ACTION_CALL, call_uri);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(intent);
        }
    }
    private static class AccessoriesViewHolder {
        ImageView sms;
        ImageView call;
    }
    private class AccessoriesAdapter extends BaseAdapter {
        @Override
        public int getCount() {
        return data.size();
        }
        @Override
        public Person getItem(int position) {
            return data.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            AccessoriesViewHolder holder = null;

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
                holder = new AccessoriesViewHolder();
                holder.sms = (ImageView) convertView.findViewById(R.id.sms);
                holder.call = (ImageView) convertView.findViewById(R.id.call);
                ((ImageView) convertView.findViewById(R.id.sms)).setOnClickListener(smsClickListener);
                ((ImageView) convertView.findViewById(R.id.call)).setOnClickListener(callClickListener);
                convertView.setTag(holder);
            }
            else {
                holder = (AccessoriesViewHolder) convertView.getTag();
            }
            //获取联系人对象
            person = data.get(position);
            // 获取contact_list布局中每个部件的试图
            TextView name = (TextView) convertView.findViewById(R.id.CONTACT_name);
            TextView phone = (TextView) convertView.findViewById(R.id.CONTACT_phone);
            ImageView sms = (ImageView) convertView.findViewById(R.id.sms);
            //设置中每个部件的显示内容
            name.setText(person.get(Person.Field.NAME));
            phone.setText(person.get(Person.Field.PHONE));
            return convertView;
        }
    }
    /**
     * Quickly shows a message to the user using a {@link Toast}.
     *
     * @param message The message to show
     */
    private void showMessage(String message) {
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
    }
    private OnClickListener smsClickListener = new OnClickListener(){
        @Override
        public void onClick(View v) {
            final int position = getListView().getPositionForView(v);
            if (position != ListView.INVALID_POSITION) {
                String phoneNumber = data.get(position).getPhone();
                sms(phoneNumber);
                //showMessage("你选中了:" + data.get(position).getPhone());
                }
        }
    };
    private OnClickListener callClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = getListView().getPositionForView(v);
            if (position != ListView.INVALID_POSITION) {
                String phoneNumber = data.get(position).getPhone();
                makeCall(phoneNumber);
                //showMessage("拨号:" + data.get(position).getPhone());
                }
        }
    };
}
