package com.example.logistics.ui;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.example.logistics.R;

public class MenuActivity extends AppCompatActivity implements View.OnClickListener{
    private LinearLayout ly_one,ly_two,ly_three;
    private TextView mTextView1,mTextView2,mTextView3;
    private TextView mTextNum1,mTextNum2,mTextNum3;
    private ImageView mImageView;

    private Fragment mFrag1;
    private Fragment mFrag2;
    private Fragment mFrag3;

    private String owner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        bindView();

        Intent intent = getIntent();
        owner = intent.getStringExtra("user");

        selectTab(0);
    }

    private void bindView() {

        ly_one = (LinearLayout)findViewById(R.id.ly_tab_menu_deal);
        ly_two = (LinearLayout)findViewById(R.id.ly_tab_menu_poi);
        ly_three = (LinearLayout)findViewById(R.id.ly_tab_menu_more);

        mTextView1 = (TextView)findViewById(R.id.tab_menu_deal);
        mTextView2 = (TextView)findViewById(R.id.tab_menu_poi);
        mTextView3 = (TextView)findViewById(R.id.tab_menu_more);

        mTextNum1 = (TextView)findViewById(R.id.tab_menu_deal_num);
        mTextNum2 = (TextView)findViewById(R.id.tab_menu_poi_num);
        mTextNum3 = (TextView)findViewById(R.id.tab_menu_more_num);


        ly_one.setOnClickListener(this);
        ly_two.setOnClickListener(this);
        ly_three.setOnClickListener(this);
    }

    //重置所有文本的选中状态
    private void setSelected() {
        mTextView1.setSelected(false);
        mTextView2.setSelected(false);
        mTextView3.setSelected(false);
    }
    @Override
    public void onClick(View v) {
        setSelected();
        switch (v.getId()) {
            case R.id.ly_tab_menu_deal:
                selectTab(0);
                break;
            case R.id.ly_tab_menu_poi:
                selectTab(1);
                break;
            case R.id.ly_tab_menu_more:
                selectTab(2);
                break;
        }
    }

    private void selectTab(int i){
        FragmentManager manager = getSupportFragmentManager();

        FragmentTransaction transaction = manager.beginTransaction();

        hideFragments(transaction);
        switch (i) {
            case 0:
                mTextView1.setSelected(true);
                mTextNum1.setVisibility(View.INVISIBLE);
                if (mFrag1 == null) {
                    mFrag1 = new SearchUI();


                    transaction.add(R.id.fragment_container, mFrag1);
                } else {
                    //如果第一页对应的Fragment已经实例化，则直接显示出来
                    transaction.show(mFrag1);
                }
                break;
            case 1:
                mTextView2.setSelected(true);
                mTextNum2.setVisibility(View.INVISIBLE);
                if (mFrag2 == null) {
                    mFrag2 = new PickUpUI();
                    Bundle bundle = new Bundle();
                    bundle.putString("user", owner);
                    mFrag2.setArguments(bundle);

                    transaction.add(R.id.fragment_container, mFrag2, "frag2");
                } else {
                    transaction.show(mFrag2);
                }
                break;
            case 2:
                mTextView3.setSelected(true);
                mTextNum3.setVisibility(View.INVISIBLE);
                if (mFrag3 == null) {
                    mFrag3 = new MeUI();
                    Bundle bundle = new Bundle();
                    bundle.putString("user", owner);
                    mFrag3.setArguments(bundle);

                    transaction.add(R.id.fragment_container, mFrag3);
                } else {
                    transaction.show(mFrag3);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFrag1 != null) {
            transaction.hide(mFrag1);
        }
        if (mFrag2 != null) {
            transaction.hide(mFrag2);
        }
        if (mFrag3 != null) {
            transaction.hide(mFrag3);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("on", "Activity");
        FragmentManager fm = getSupportFragmentManager();
        Fragment fr = fm.findFragmentByTag("frag2");
        fr.onActivityResult(requestCode, resultCode, data);
    }

    public String getOwner(){
        return owner;
    }

}
