package com.ifavine.pay.ui.activity;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ifavine.pay.R;
import com.ifavine.pay.adapter.SpinnerAdapter;
import com.ifavine.pay.ui.base.BaseActivity;
import com.ifavine.pay.utils.BaseUtil;
import com.ifavine.pay.utils.DialogMainSetUtils;
import com.ifavine.pay.utils.DialogUtil;
import com.ifavine.pay.view.PopWindowMainReport;
import com.ifavine.pay.view.PopWindowMainSet;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 */


public class MainActivity extends BaseActivity {
    @BindView(R.id.tv_date)
    TextView tv_date;

    @BindView(R.id.tv_set)
    Button tv_set;

    @BindView(R.id.tv_table)
    Button tv_table;


    private int startMin;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        startMin = 0;
    }

    @Override
    public void initData() {
        super.initData();
        date = System.currentTimeMillis();
        startTimer();
        //检查网络
        pingNet("www.baidu.com");
    }

    private boolean pingNet(String address) {
        try {
            Process process = Runtime.getRuntime().exec("ping -c 3 -w 100 " + address);
            InputStreamReader r = new InputStreamReader(process.getInputStream());
            LineNumberReader returnData = new LineNumberReader(r);
            String returnMsg = "";
            String line = "";
            while ((line = returnData.readLine()) != null) {
                System.out.println(line);
                returnMsg += line;
            }
            if (TextUtils.isEmpty(returnMsg)) {
                System.out.println("与 " + address + " 连接不畅通.");
                return false;
            } else {
                //0 received, 100% packet loss
                if (returnMsg.indexOf("0 received") == 1 || returnMsg.indexOf("100% packet loss") == 1) {
                    System.out.println("与 " + address + " 连接不畅通.");
                    return false;
                } else {
                    System.out.println("与 " + address + " 连接畅通.");
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @OnClick({R.id.tv_set, R.id.tv_table, R.id.tv_close, R.id.tv_reflesh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_set:
                new PopWindowMainSet(mContext, new PopWindowMainSet.CallBackPosition() {
                    @Override
                    public void selectIndex(int i) {
                        if (i == 0) {
                            DialogMainSetUtils.showRSTSetDialog(mContext, "饭堂窗口设置", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                }
                            }, null, true);
                        } else if (i == 1) {
                            DialogMainSetUtils.showSetAmtDialog(mContext, "消费金额设置", null, null, true);
                        } else if (i == 2) {
                            DialogMainSetUtils.showChangePwdDialog(mContext, "修改管理员密码", null, null, true);
                        }
                    }
                }).showAtBottom(tv_set);
                break;
            case R.id.tv_table:
                new PopWindowMainReport(mContext, new PopWindowMainReport.CallBackPosition() {
                    @Override
                    public void selectIndex(int index) {
                        if (index == 0) {
                            DialogMainSetUtils.showReportLocalDialog(mContext, "本机消费记录查询", null, true);
                        } else if (index == 1) {
                            DialogMainSetUtils.showReportRecordDialog(mContext, "消费记录查询", null, true);
                        } else if (index == 2) {
                            DialogMainSetUtils.showReportSumDialog(mContext, "每月消费汇总查询", null, true);
                        }

                    }
                }).showAtBottom(tv_table);
                break;
            case R.id.tv_close:
                DialogUtil.showConfirmDialog(mContext, null, "系统提示", "确认关闭应用?", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        System.exit(0);
                    }
                }, null, true);
                break;
            case R.id.tv_reflesh:

                break;
        }
    }


    // 初始化定时器
    private long date;
    private Timer timer;

    private void startTimer() {
        if (timer != null) {
            return;
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                date += 1000;
                handler.sendEmptyMessage(1);
            }
        }, 1000, 1000);
    }

    // 停止定时器
    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            // 一定设置为null，否则定时器不会被回收
            timer = null;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tv_date.setText(BaseUtil.getDateFormat(date, "yyyy-MM-dd HH:mm:ss"));
                    break;
            }
        }
    };

}
