package com.example.administrator.zhapp.weight;

import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.NetWorkType;

import java.util.ArrayList;
import java.util.List;

import static com.example.administrator.zhapp.R.id.sp_networkstate;

/**
 * Created by Administrator on 2016/10/22.
 */

public class NetWorkStateDialog {

    private Context mContext;
    private AlertDialog dialog;
    private TextView tv_title;
    private Spinner sp_networktype;
    private EditText et_ip;
    private Button btn_qr;
    private Button btn_qx;
    private Window mWindow;
    private List<NetWorkType> lists;
    private MyAdapter adapter;

    public NetWorkStateDialog(Context context) {
        mContext = context;

        dialog = new AlertDialog.Builder(context).create();
        dialog.setCancelable(true);
        dialog.show();
        mWindow = dialog.getWindow();
        mWindow.setContentView(R.layout.networdstate_dialog);
        tv_title = (TextView) mWindow.findViewById(R.id.tv_title);
        btn_qr = (Button) mWindow.findViewById(R.id.btn_qr);
        btn_qx = (Button) mWindow.findViewById(R.id.btn_qx);
        et_ip = (EditText) mWindow.findViewById(R.id.et_ip);
        sp_networktype = (Spinner) mWindow.findViewById(sp_networkstate);

        initSpinner();
    }

    private void initSpinner() {

        lists = new ArrayList<>();
        NetWorkType type = new NetWorkType(NetWorkType.Type.WIFI, Contans.IP);
        NetWorkType type1 = new NetWorkType(NetWorkType.Type.MOBILE, "http://10.2.2.200:8082");
        lists.add(type);
        lists.add(type1);
        adapter = new MyAdapter();
        sp_networktype.setAdapter(adapter);
        sp_networktype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                et_ip.setText(lists.get(position).getIp());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    /**
     * 相加点击事件
     *
     * @param listener
     */
    public void setqrOnClick(View.OnClickListener listener) {
        btn_qr.setOnClickListener(listener);
    }

    /**
     * 取消点击事件
     *
     * @param listener
     */
    public void setQxOnClick(View.OnClickListener listener) {
        btn_qx.setOnClickListener(listener);
    }

    /**
     * 设置Title
     *
     * @param title
     */
    public void setTitle(String title) {
        tv_title.setText(title);
    }


    public void close() {
        dialog.dismiss();
    }

    class MyAdapter extends BaseAdapter {

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.spinner_item, null, false);
                holder.tv = (TextView) convertView.findViewById(R.id.item);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (lists.get(position).getType() == NetWorkType.Type.WIFI) {
                holder.tv.setText("WIFI");
            } else {
                holder.tv.setText("4G");
            }
            return convertView;
        }
    }

    private final class ViewHolder {
        TextView tv;
    }
}
