package com.naz.sdkdemo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.naz.sdkdemo.activity.AdActivity;
import com.naz.sdkdemo.activity.ApplyTrademarkActivity;
import com.naz.sdkdemo.activity.FullApplyTrademarkActivity;
import com.naz.sdkdemo.activity.RecycleTrademarkActivity;
import com.naz.sdkdemo.activity.RepairTrademarkActivity;
import com.naz.sdkdemo.activity.SaveTrademarkActivity;
import com.naz.sdkdemo.activity.SettingActivity;
import com.naz.sdkdemo.activity.StockSearchActivity;
import com.naz.sdkdemo.activity.UpTrademarkActivity;
import com.naz.sdkdemo.activity.VerifyTrademarkActivity;
import com.naz.sdkdemo.activity.WaitActivity;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.bean.UserDetail;
import com.naz.sdkdemo.comm.Constants;
import com.naz.sdkdemo.helper.SerialPortManager;
import com.naz.sdkdemo.utils.SerialUtil;
import com.naz.sdkdemo.weight.ActivityManager;
import com.naz.sdkdemo.weight.UserCache;
import com.naz.sdkdemo.weight.XToast;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;


public class NewMainActivity extends BaseActivity {

    private boolean canClick = false;
    private boolean showAd = false;
    private Timer timer;
    private TimerTask timerTask;

    @Override
    protected boolean initBundle(Bundle bundle) {
        return super.initBundle(bundle);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.new_main_activity;
    }

    @Override
    protected void initView() {
        timer = new Timer();
        findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, SettingActivity.class));
            }
        });
        setInfo();
        findViewById(R.id.type_cb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, SaveTrademarkActivity.class));
            }
        });
        findViewById(R.id.type_sb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, UpTrademarkActivity.class));
            }
        });
        findViewById(R.id.type_zjlb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, FullApplyTrademarkActivity.class));
            }
        });
        findViewById(R.id.type_lb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, ApplyTrademarkActivity.class));
            }
        });
        findViewById(R.id.type_bb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, RepairTrademarkActivity.class));
            }
        });
        findViewById(R.id.type_hs).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, RecycleTrademarkActivity.class));
            }
        });
        findViewById(R.id.type_cx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, StockSearchActivity.class));
            }
        });
        findViewById(R.id.type_jy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
                startActivity(new Intent(NewMainActivity.this, VerifyTrademarkActivity.class));
            }
        });
    }

    private void click() {
        Log.e("--------->","--------->click");
        if (timerTask != null){
            timerTask.cancel();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("--------->","--------->onResume");
        timerTask = new TimerTask() {
            @Override
            public void run() {
                Log.e("--------->","--------->run");
                startActivity(new Intent(NewMainActivity.this, AdActivity.class));
            }
        };
        timer.schedule(timerTask, 10*60*1000);
    }

    private void setInfo() {
        TextView tvName = findViewById(R.id.name);
        TextView tvPhone = findViewById(R.id.phone);
        UserDetail user = UserCache.getUserInfoDetail();
        if (null != user) {
            tvName.setText(String.format("驻点人员姓名：%s", user.name));
            tvPhone.setText(String.format("驻点人员手机：%s", user.phone));
        }
    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == 139 || keyCode == 293 || keyCode == 280) {
//            if (event.getRepeatCount() == 0) {
//                if (curTabId.equals("首页")) {
//                    HomeFragment fragment = (HomeFragment) manager.findFragmentByTag("首页");
//                    fragment.startStop();
//                } else if (curTabId.equals("安装")) {
//                    MiddleFragment fragment = (MiddleFragment) manager.findFragmentByTag("安装");
////                    fragment.startStop();
//                }else if (curTabId.equals("发/收货")) {
//                    TakeGoodsFragment fragment = (TakeGoodsFragment) manager.findFragmentByTag("发/收货");
//                    fragment.startStop();
//                }else if (curTabId.equals("扫描")) {
//                    ScannerFragment fragment = (ScannerFragment) manager.findFragmentByTag("扫描");
//                    fragment.startStop();
//                }
//            }
//            return true;
//        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
//            MyFinish();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    long firstTime = 0;

    private void MyFinish() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) { // 如果两次按键时间间隔大于2秒，则不退出
            XToast.showToast("再按一次退出程序");
            firstTime = secondTime;// 更新firstTime
        } else { // 两次按键小于2秒时，退出应用
            ActivityManager.getActivityManager().exitApp();
            System.exit(0);
        }
    }
}
