package it.integry.integrywmsnative.gest.main;


import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.cachapa.expandablelayout.ExpandableLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.REST.watcher.ServerStatusChecker;
import it.integry.integrywmsnative.core.interfaces.ITitledFragment;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.wifi.WiFiCheckerViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MainFragment extends Fragment implements ITitledFragment {


    @BindView(R.id.no_connection_top_layout) ExpandableLayout mNoConnectionLayout;
    @BindView(R.id.current_user_name) TextView currentUsername;
    //@BindView(R.id.drawer_username) TextView drawerUsername;

    private View mView;

    public MainFragment() {
        // Required empty public constructor
    }

    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_main, container, false);

        ButterKnife.bind(this, mView);

        init();
        return mView;
    }


    private void init(){
        ServerStatusChecker.getIstance().addCallback(value -> {
            if(value && mNoConnectionLayout.isExpanded()){

                collapseNoConnectionLayout();


            } else if(!value && !mNoConnectionLayout.isExpanded()){
                expandNoConnectionLayout();
            }
        });

        WiFiCheckerViewHolder wiFiCheckerViewHolder = new WiFiCheckerViewHolder(getActivity(), mView, R.id.wifi_power);
        wiFiCheckerViewHolder.startMonitoring();


        currentUsername.setText(SettingsManager.i().user.fullname);
        //drawerUsername.setText(SettingsManager.i().user.fullname);

    }

    private void collapseNoConnectionLayout(){
        if(getActivity() != null) getActivity().runOnUiThread(() -> mNoConnectionLayout.collapse(true));
    }

    private void expandNoConnectionLayout(){
        if(getActivity() != null) getActivity().runOnUiThread(() -> mNoConnectionLayout.expand(true));
    }


    @Override
    public String getTitle(Context context) {
        return context.getString(R.string.app_name);
    }
}
