package com.virjar.echo.adr.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

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

import com.virjar.echo.adr.HttpProxyService;
import com.virjar.echo.adr.R;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static void go(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    LinearLayout logLinear;

    LinearLayout settingLinear;

    LinearLayout userLinear;


    private FragmentManager mfragmentManger;
    private LogFragment logFragment;
    private SettingsActivity.SettingsFragment settingsFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        logLinear = findViewById(R.id.main_log);
        settingLinear = findViewById(R.id.main_setting);
        userLinear = findViewById(R.id.linear_user);


        mfragmentManger = getSupportFragmentManager();
        logLinear.performClick();

        startService(new Intent(this, HttpProxyService.class));
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction fragmentTransaction = mfragmentManger.beginTransaction();//只能是局部变量，不能为全局变量，否则不能重复commit
        //FragmentTransaction只能使用一次
        hideAllFragment(fragmentTransaction);
        if (R.id.main_log != view.getId() && logFragment != null) {
            logFragment.stop();
        }
        switch (view.getId()) {
            case R.id.main_log:
                setAllFalse();
                logLinear.setSelected(true);
                if (logFragment == null) {
                    logFragment = new LogFragment();
                    fragmentTransaction.add(R.id.fragment_frame, logFragment);
                } else {
                    fragmentTransaction.show(logFragment);
                }
                logFragment.start();
                break;
            case R.id.main_setting:
                setAllFalse();
                settingLinear.setSelected(true);
                if (settingsFragment == null) {
                    settingsFragment = new SettingsActivity.SettingsFragment();
                    fragmentTransaction.add(R.id.fragment_frame, settingsFragment);
                } else {
                    fragmentTransaction.show(settingsFragment);
                }
                break;
            case R.id.linear_user:
                setAllFalse();
                userLinear.setSelected(true);
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.fragment_frame, mineFragment);
                } else {
                    fragmentTransaction.show(mineFragment);
                }
                break;
        }
        fragmentTransaction.commit();//记得必须要commit,否则没有效果
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (logFragment != null) {
            fragmentTransaction.hide(logFragment);
        }
        if (settingsFragment != null) {
            fragmentTransaction.hide(settingsFragment);
        }

        if (mineFragment != null) {
            fragmentTransaction.hide(mineFragment);
        }

    }

    private void setAllFalse() {
        logLinear.setSelected(false);
        settingLinear.setSelected(false);
        userLinear.setSelected(false);
    }
}
