package cn.dankal.yunlaboratory.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.mvp.BasePresenter;
import cn.dankal.yunlaboratory.R;
import cn.dankal.yunlaboratory.ui.fragment.PersonFragment;
import cn.dankal.yunlaboratory.ui.fragment.ScanFragment;

public class MainActivity extends BaseActivity {


    @BindView(R.id.iv_scan)
    ImageView ivScan;
    @BindView(R.id.tv_scan)
    TextView tvScan;
    @BindView(R.id.linear_scan)
    LinearLayout linearScan;
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.linear_mine)
    LinearLayout linearMine;

    private List<Fragment> fragments;

    private int lastShowFragmentIndex = 0;

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initComponents() {

        fragments = new ArrayList<>();

        fragments.add(ScanFragment.getInstance());

        fragments.add(PersonFragment.getInstance());


        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        for (int i = 0; i < fragments.size(); i++) {
            transaction.add(R.id.frame, fragments.get(i));
            transaction.hide(fragments.get(i));
        }


        transaction.show(fragments.get(0));

        transaction.commit();
    }


    @OnClick({R.id.linear_scan, R.id.linear_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_scan:
                changeFragment(0);
                break;
            case R.id.linear_mine:
                changeFragment(1);
                break;
        }
    }

    private void changeFragment(int index) {
        if (index == lastShowFragmentIndex)
            return;
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragments.get(lastShowFragmentIndex));
        transaction.show(fragments.get(index));

        transaction.commit();

        lastShowFragmentIndex = index;

        switch (index) {
            case 0:
                ivScan.setImageResource(R.drawable.ic_scan_press);
                tvScan.setTextColor(getResources().getColor(R.color.color6fc3ff));


                ivMine.setImageResource(R.drawable.ic_mine);
                tvMine.setTextColor(getResources().getColor(R.color.colorABB2B7));
                break;
            case 1:

                ivScan.setImageResource(R.drawable.ic_scan);
                tvScan.setTextColor(getResources().getColor(R.color.colorABB2B7));

                ivMine.setImageResource(R.drawable.ic_mine_press);
                tvMine.setTextColor(getResources().getColor(R.color.color6fc3ff));


                break;
            case 2:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (fragments != null) {
            for (Fragment fragment : fragments) {

                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}