package com.phlx.anchorcollect.ui.main;

import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.phlx.anchorcollect.BR;
import com.phlx.anchorcollect.Configs;
import com.phlx.anchorcollect.R;
import com.phlx.anchorcollect.data.VMFactory;
import com.phlx.anchorcollect.databinding.ActivityMainBinding;
import com.phlx.anchorcollect.rfid.ModuleManager;
import com.phlx.anchorcollect.ui.base.BaseActivity;
import com.phlx.anchorcollect.ui.fragment.BasicsCollectFragment;
import com.phlx.anchorcollect.ui.fragment.BreedingRecordFragment;
import com.phlx.anchorcollect.ui.fragment.CollectFragment;
import com.phlx.anchorcollect.ui.fragment.CollectListFragment;
import com.phlx.anchorcollect.ui.fragment.PerformanceFragment;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainVM> {

    private String collectType = "";

    private FragmentTransaction transaction;

    private BasicsCollectFragment _basicsCollectFragment;
    private PerformanceFragment _performanceFragment;
    private BreedingRecordFragment _breedingRecordFragment;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null) {
            collectType = mBundle.getString("COLLECT_TYPT");
        }
    }

    @Override
    public MainVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MainVM.class);
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.init();

        //初始化标题
//        viewModel.initToolbar(getTitleName());
        //初始化Fragment
        initFragment();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.onTabClickEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                labelSwitching(integer);
            }
        });
    }

    private void initFragment() {

        transaction = getSupportFragmentManager().beginTransaction();

        _basicsCollectFragment = BasicsCollectFragment.getInstance();
        _performanceFragment = PerformanceFragment.getInstance();
        _breedingRecordFragment = BreedingRecordFragment.getInstance();
        transaction.add(R.id.frameLayout, _basicsCollectFragment);
        transaction.add(R.id.frameLayout, _performanceFragment);
        transaction.add(R.id.frameLayout, _breedingRecordFragment);
        transaction.commit();
        labelSwitching(0);
    }

    public void labelSwitching(int position) {

        transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(_basicsCollectFragment);
        transaction.hide(_performanceFragment);
        transaction.hide(_breedingRecordFragment);
        binding.tvTab0.setTextColor(getResources().getColor(R.color.color_font_non_focus));
        binding.tvTab1.setTextColor(getResources().getColor(R.color.color_font_non_focus));
        binding.tvTab5.setTextColor(getResources().getColor(R.color.color_font_non_focus));
        switch (position) {
            case 0:
                transaction.show(_basicsCollectFragment);
                binding.tvTab0.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 1:
                transaction.show(_performanceFragment);
                binding.tvTab1.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 2:
//                transaction.show(_performanceFragment);
//                binding.tvTab1.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 3:
//                transaction.show(_performanceFragment);
//                binding.tvTab1.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 4:
//                transaction.show(_performanceFragment);
//                binding.tvTab1.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 5:
                transaction.show(_breedingRecordFragment);
                binding.tvTab5.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
        }

        transaction.commit();
    }

    /**
     * 监听Back键按下事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            MessageDialog.show("系统提示", "是否退出程序?", "确定", "取消")
//                    .setOkButton(new OnDialogButtonClickListener<MessageDialog>() {
//                        @Override
//                        public boolean onClick(MessageDialog baseDialog, View v) {
//                            ModuleManager.destroyLibSO();
//                            finish();
//                            android.os.Process.killProcess(android.os.Process.myPid());
//                            return false;
//                        }
//                    })
//            .setCancelButton(new OnDialogButtonClickListener<MessageDialog>() {
//                @Override
//                public boolean onClick(MessageDialog baseDialog, View v) {
//                    return false;
//                }
//            })
//            ;


            new MaterialDialog.Builder(this).title("系统提示").content("是否退出程序?").positiveText("确定").negativeText("取消").onAny(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        ModuleManager.destroyLibSO();
                        finish();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                }
            }).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
