package com.police.policedatasystem;


import static com.police.policedatasystem.util.UaCredentialApi.ACTION_SUFFIX_UA_LOGIN;
import static com.police.policedatasystem.util.UaCredentialApi.ACTION_UA_LOGOUT;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.police.policedatasystem.databinding.ActivityIndexBinding;
import com.police.policedatasystem.fragment.ApplyFragment;
import com.police.policedatasystem.fragment.DataFragment;
import com.police.policedatasystem.fragment.MineFragment;
import com.police.policedatasystem.http.RequestClient;
import com.police.policedatasystem.util.Constants;
import com.police.policedatasystem.viewmodel.IndexViewModel;

public class indexActivity extends AppCompatActivity {
    public ActivityIndexBinding binding;
    private IndexViewModel viewModel;
    public RequestClient requestClient;
    public boolean findAddress = false;
    private final  DataFragment dataFragment = new DataFragment();
    private final ApplyFragment applyFragment= new ApplyFragment();
    private final MineFragment mineFragment= new MineFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityIndexBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        viewModel = new IndexViewModel(this);
        requestClient = new RequestClient(this);
    }

    private void initView() {
        binding.navBarGroup.navBar1.setOnClickListener(view -> {
            replaceFragment(dataFragment);
            binding.navBarGroup.navBarIcon1.setImageResource(R.mipmap.icon_data_selected);
            binding.navBarGroup.navBarText1.setTextColor(getColor(R.color.selected_color));
            binding.navBarGroup.navBarIcon2.setImageResource(R.mipmap.icon_apply_normal);
            binding.navBarGroup.navBarText2.setTextColor(getColor(R.color.normal_color));
            binding.navBarGroup.navBarIcon3.setImageResource(R.mipmap.icon_mine_normal);
            binding.navBarGroup.navBarText3.setTextColor(getColor(R.color.normal_color));
        });
        binding.navBarGroup.navBar2.setOnClickListener(view -> {
            replaceFragment(applyFragment);
            binding.navBarGroup.navBarIcon1.setImageResource(R.mipmap.icon_data_normal);
            binding.navBarGroup.navBarText1.setTextColor(getColor(R.color.normal_color));
            binding.navBarGroup.navBarIcon2.setImageResource(R.mipmap.icon_apply_selected);
            binding.navBarGroup.navBarText2.setTextColor(getColor(R.color.selected_color));
            binding.navBarGroup.navBarIcon3.setImageResource(R.mipmap.icon_mine_normal);
            binding.navBarGroup.navBarText3.setTextColor(getColor(R.color.normal_color));
        });
        binding.navBarGroup.navBar3.setOnClickListener(view -> {
            replaceFragment(mineFragment);
            binding.navBarGroup.navBarIcon1.setImageResource(R.mipmap.icon_data_normal);
            binding.navBarGroup.navBarText1.setTextColor(getColor(R.color.normal_color));
            binding.navBarGroup.navBarIcon2.setImageResource(R.mipmap.icon_apply_normal);
            binding.navBarGroup.navBarText2.setTextColor(getColor(R.color.normal_color));
            binding.navBarGroup.navBarIcon3.setImageResource(R.mipmap.icon_mine_selected);
            binding.navBarGroup.navBarText3.setTextColor(getColor(R.color.selected_color));
        });
        replaceFragment(dataFragment);
        registerBroadcastReceiver();
    }

    public void initData() {
        //改变寻址状态，走到这里证明寻址成功了
        if (findAddress) return;
        findAddress = true;
        binding.wmWm.setTextSize(50);
        binding.wmWm.setText(Constants.SFZH);
        dataFragment.initData();
    }

    private void replaceFragment(Fragment newFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fc_view, newFragment);
        transaction.commit();
    }

    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        String pkgName = getPackageName();
        /**添加登录成功事件监听
         * 当第一次登录，调用【IF-UA-SDK-01】接口可能返回"票据不存在"，此时需要注册监听此事件，
         * 当登录成功后，接收到此事件即可重新调用【IF-UA-SDK-01】获取相关票据
         * */
        intentFilter.addAction(pkgName + ACTION_SUFFIX_UA_LOGIN);

        /**增加登出事件监听
         * 当UA主动账户退出时会发送该事件，此时已经登录应用可以根据自身业务实施退出
         * */
        intentFilter.addAction(pkgName + ACTION_UA_LOGOUT);
        registerReceiver(uaLoginInReceiver, intentFilter);
    }

    private final BroadcastReceiver uaLoginInReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.isEmpty(action)) {
                return;
            }
            if (action.contains(ACTION_SUFFIX_UA_LOGIN)) {
                //再次获取票据
                if (requestClient != null) {
                    requestClient.init();
                }
                return;
            }
            if (action.contains(ACTION_UA_LOGOUT)) {
                //程序退出
                startActivity(new Intent(indexActivity.this, indexActivity.class));
                finish();
            }
        }
    };
}