package com.example.tyre;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.tyre.databinding.ActivityHomePageBinding;
import com.example.tyre.maintenance.NewHomePageActivity;

/**
 * Created by 杨万里 on 2019/12/3.
 */

public class HomePageActivity extends AppCompatActivity {

    RadioButton chaxun;
    RadioButton ruku;
    RadioButton chuku;
    RadioButton zhuangxie;
    RadioButton huanwei;
    RadioButton zhijian;
    Button logout;
    private ActivityHomePageBinding binding;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomePageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        bindViews();
        setupClickListeners();
    }

    private void bindViews() {
        chaxun = binding.chaxun;
        ruku = binding.ruku;
        chuku = binding.chuku;
        zhuangxie = binding.zhuangxie;
        huanwei = binding.huanwei;
        // zhijian = binding.zhijian;
        logout = binding.logout;
    }

    private void setupClickListeners() {
        binding.chaxun.setOnClickListener(v -> {
            Intent intent = new Intent(this, TyreSearchActivity.class);
            startActivity(intent);
        });

        binding.ruku.setOnClickListener(v -> {
            Intent intent = new Intent(this, InStoreHouseActivity.class);
            startActivity(intent);
        });
        Intent outStoreIntent = new Intent(this, OutStoreHouseActivity.class);
        binding.chuku.setOnClickListener(v -> {
            outStoreIntent.putExtra("title","轮胎出库");
            startActivity(outStoreIntent);
        });
        binding.chuku1.setOnClickListener(v -> {
            outStoreIntent.putExtra("title","调拨出库");
            startActivity(outStoreIntent);
        });

        binding.zhuangxie.setOnClickListener(v -> {
            Intent intent = new Intent(this, UpActivity.class);
            startActivity(intent);
        });

        binding.huanwei.setOnClickListener(v -> {
            Intent intent = new Intent(this, DownActivity.class);
            startActivity(intent);
        });

        // binding.zhijian.setOnClickListener(v -> {
        //     Intent intent = new Intent(this, CheckActivity.class);
        //     startActivity(intent);
        // });

        binding.xxbd.setOnClickListener(v -> {
            Intent intent = new Intent(this, TestActivity.class);
            startActivity(intent);
        });

        binding.test1.setOnClickListener(v -> {
            Intent intent = new Intent(this, TyreLayoutActivity.class);
            startActivity(intent);
        });

        binding.logout.setOnClickListener(v -> {
            handleLogout();
        });
        binding.inNewSystem.setOnClickListener(v -> {
            Intent intent = new Intent(this, NewHomePageActivity.class);
            startActivity(intent);
        });
    }

    /**
     * 新增：处理退出登录的逻辑
     */
    private void handleLogout() {
        // 1. 清除登录状态（可选：清除 SharedPreferences 中的用户信息）
        // 例如：SharedPreferencesUtils.clear();

        // 2. 提示用户
        Toast.makeText(this, "已退出登录", Toast.LENGTH_SHORT).show();

        // 3. 跳转回登录页 (MainActivity)
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

        // 4. 关闭当前主页，防止按返回键回到主页
        finish();
    }

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            // System.exit(0); // 通常不需要手动调用 System.exit，finish() 即可
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}