package com.example.bpa;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.bpa.config.ConfigName;
import com.example.bpa.view.fragment.CloudFragment;
import com.example.bpa.view.fragment.HeplerFragment;
import com.example.bpa.view.fragment.HomeFragment;
import com.example.bpa.view.fragment.SystemCapabilitiesFragment;
import com.example.bpa.view.fragment.SystemSetFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener{

    //region 界面变量
    //标题
    private TextView clist_title;
    //返回 云端 系统功能 系统设置 系统帮助
    private ImageView CloudUpdates,SystemCapabilities,SystemSettings,SystemHelp,HomeMain,ColseMain;
    //页面
    private FrameLayout fragment_container;

    //endregion

    //region 界面
    //主页
    public HomeFragment homeFragment=new HomeFragment();
    //更新
    public CloudFragment cloudFragment=new CloudFragment();
    //系统功能
    public SystemCapabilitiesFragment systemCapabilitiesFragment=new SystemCapabilitiesFragment();
    //系统设置
    public SystemSetFragment systemSetFragment=new SystemSetFragment();
    //系统帮助
    public HeplerFragment heplerFragment=new HeplerFragment();
    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Init();
        initEvents();
    }
    //endregion

    //region 公有函数
    /**
     * 初始化
     */
    private void Init() {
        clist_title = findViewById(R.id.clist_title);
        CloudUpdates = findViewById(R.id.CloudUpdates);
        SystemCapabilities = findViewById(R.id.SystemCapabilities);
        SystemSettings = findViewById(R.id.SystemSettings);
        SystemHelp = findViewById(R.id.SystemHelp);
        HomeMain= findViewById(R.id.HomeMain);
        ColseMain= findViewById(R.id.ColseMain);
        fragment_container= findViewById(R.id.fragment_container);
        clist_title.setText(ConfigName.getInstance().Shop_Name);
        //ShowFragment(homeFragment,"系统主页");
        ShowFragment(systemCapabilitiesFragment,"功能菜单");
    }
    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        //设置四个Tab的点击事件
        CloudUpdates.setOnClickListener(this);
        SystemCapabilities.setOnClickListener(this);
        SystemSettings.setOnClickListener(this);
        SystemHelp.setOnClickListener(this);
        HomeMain.setOnClickListener(this);
        ColseMain.setOnClickListener(this);
    }
    //endregion

    //region 点击事件
    /**
     * 本页面点击事件监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.CloudUpdates://系统更新按钮
                ShowFragment(cloudFragment,"云端更新");
                break;
            case R.id.SystemCapabilities://系统功能按钮
                ShowFragment(systemCapabilitiesFragment,"功能菜单");
                break;
            case R.id.SystemSettings://系统设置按钮
                ShowFragment(systemSetFragment,"系统设置");
                break;
            case R.id.SystemHelp://系统帮助按钮
                ShowFragment(heplerFragment,"系统帮助");
                break;
            case R.id.HomeMain://主页
                ShowFragment(homeFragment,"系统主页");
                break;
            case R.id.ColseMain://关闭主窗体
                ColseActive();
                break;
        }
    }

    /**
     * 当前选中窗体
     */
    public Fragment SelectFragment=null;
    /**
     * 打开窗体
     */
    public void ShowFragment(Fragment fragment,String Text)
    {
        clist_title.setText(Text);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if(SelectFragment!=null)
        {
            transaction.remove(SelectFragment);
        }
        transaction.add(R.id.fragment_container, fragment);
        transaction.commit();
        SelectFragment=fragment;
    }

    /**
     * 关闭主窗体
     */
    public void ColseActive()
    {
        // 1.创建弹框对象,显示在当前页面
        AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);
        // 2.编辑弹框样式
        // 2.1 创建标题
        ab.setTitle("退出安全提示");
        // 2.3 设置图标
        ab.setIcon(R.mipmap.ts);
        // 2.4 设置内容
        ab.setMessage("您是否确定退出程序？");
        // 2.5 设置按钮
        ab.setPositiveButton("取消",null);
        ab.setNeutralButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 实现程序的退出，结束当前
                MainActivity.this.finish();
            }
        });
        // 3.创建弹框
        ab.create();
        // 4.显示弹框
        ab.show();
    }
    //endregion
}