package com.sipai.jspatrol.Equ;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gigamole.navigationtabstrip.NavigationTabStrip;
import com.sipai.jspatrol.Bean.EquList;
import com.sipai.jspatrol.Equ.Equfrgment.EquDetailFragment;
import com.sipai.jspatrol.Equ.Equfrgment.EquDocumentFragment;
import com.sipai.jspatrol.Equ.Equfrgment.EquFaultListFragment;
import com.sipai.jspatrol.Equ.Equfrgment.EquMaintainFragment;
import com.sipai.jspatrol.R;

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

public class EquCardActivity extends AppCompatActivity {
    private NavigationTabStrip tab;
    private String[] tab_title = {"设备详情","维修记录","保养记录","测量点"};
    private ViewPager vp;
    private EquList equ;

    public static void actionStart(Context context, EquList equList){
        Intent intent = new Intent(context, EquCardActivity.class);
        intent.putExtra("equ",equList);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equ_card);
        init();
        initView();
    }

    private void init() {
        if (getIntent().getSerializableExtra("equ")!=null){
            equ = (EquList) getIntent().getSerializableExtra("equ");
        }
    }

    private void initView() {
        TextView tv_title = findViewById(R.id.moudle_back_title_tv_title);
        tv_title.setText("设备详情");
        ImageView btn_back = findViewById(R.id.moudle_back_title_iv_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tab = findViewById(R.id.moudle_equ_detail_tab);
        tab.setTitles(tab_title);
        tab.setTabIndex(0);
        tab.setOnTabStripSelectedIndexListener(new NavigationTabStrip.OnTabStripSelectedIndexListener() {
            @Override
            public void onStartTabSelected(String title, int index) {
                vp.setCurrentItem(index);
            }

            @Override
            public void onEndTabSelected(String title, int index) {

            }
        });
        vp = findViewById(R.id.moudle_equ_detail_vp);
        final List<Fragment> list = new ArrayList<>();
        list.add(new EquDetailFragment());
        list.add(new EquFaultListFragment());
        list.add(new EquMaintainFragment());
        list.add(new EquDocumentFragment());
        vp.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }


        });
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tab.setTabIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public EquList getEquList(){
        return equ;
    }

}
