package com.example.beijing_daxing;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.example.beijing_daxing.adapter.CheckListAdapter;
import com.example.beijing_daxing.adapter.HomePageAdapter;
import com.example.beijing_daxing.base.MyApplication;
import com.example.beijing_daxing.broadcast.MyNetWorkReceiver;
import com.example.beijing_daxing.databinding.ActivityHomePageBinding;
import com.example.beijing_daxing.been.HomeIcon;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

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

public class HomePageActivity extends AppCompatActivity {
    private MyNetWorkReceiver myNetWorkReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityHomePageBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_home_page);
        initUi(binding);
       /* myNetWorkReceiver=new MyNetWorkReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(myNetWorkReceiver, intentFilter);*/

    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 4) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        var uhfLongerManager = MyApplication.getUhfLongerManager();
        uhfLongerManager.asyncStopReading();
        uhfLongerManager.close();
        MyApplication.uhfLongerManager = null;
        Log.e("TAG", "关闭" );
        // unregisterReceiver(myNetWorkReceiver);
    }

    private void initUi(ActivityHomePageBinding binding) {
        List<HomeIcon> list = new ArrayList<>();
        list.add(new HomeIcon(R.mipmap.home_in1, "轮挡入库", InActivity.class));
        list.add(new HomeIcon(R.mipmap.home_out1, "轮挡领用出库", OutActivity.class));
        list.add(new HomeIcon(R.mipmap.home_store_check, "仓库盘点", StoreCheckActivity.class));
        list.add(new HomeIcon(R.mipmap.home_check, "轮挡巡检", CheckListActivity.class));
        list.add(new HomeIcon(R.mipmap.home_bf, "轮挡报废", BFActivity.class));
        list.add(new HomeIcon(R.mipmap.home_in, "废品入库", FpInActivity.class));
        list.add(new HomeIcon(R.mipmap.home_out, "废品出库", FpOutActivity.class));
        list.add(new HomeIcon(R.mipmap.home_out, "功率设置", SetingPowerActivity.class));
        var intent = getIntent();
        try {
            if (!intent.getStringExtra("user").equals("admin")) {
                ArrayList<String> strings = new Gson().fromJson(intent.getStringExtra("json"), new TypeToken<ArrayList<String>>() {
                }.getType());
                if (strings != null) {
                    Iterator<HomeIcon> iter = list.iterator();
                    while (iter.hasNext()) {
                        HomeIcon homeIcon = iter.next();
                        if (!strings.contains(homeIcon.getName())) {
                            iter.remove();
                        }
                    }
                } else {
                    list.clear();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        HomePageAdapter adapter = new HomePageAdapter(list, this);
        binding.homeGridview.setAdapter(adapter);
        binding.homeGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HomePageActivity.this, adapter.getItem(i));
                startActivity(intent);
            }
        });
    }
}