package com.cpm.lorealbaMabeline.fragements;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cpm.lorealbaMabeline.Database.Lorealba_Database;
import com.cpm.lorealbaMabeline.DealarBoardActivity;
import com.cpm.lorealbaMabeline.R;
import com.cpm.lorealbaMabeline.adapter.MyPager;
import com.cpm.lorealbaMabeline.constant.CommonFunctions;
import com.cpm.lorealbaMabeline.constant.CommonString;
import com.cpm.lorealbaMabeline.gsonGetterSetter.MasterPromoOffer;

import java.io.File;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import me.relex.circleindicator.CircleIndicator;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link MonthlyOfferFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MonthlyOfferFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private ViewPager viewPager;
    private CircleIndicator circleIndicator;
    private MyPager myPager;
    Lorealba_Database db;
    Timer timer;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MonthlyOfferFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MonthlyOfferFragment newInstance(String param1, String param2) {
        MonthlyOfferFragment fragment = new MonthlyOfferFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_monthly_offer, container, false);
        viewPager = view.findViewById(R.id.view_pager);
        db = new Lorealba_Database(getContext());
        final ArrayList<MasterPromoOffer>[] masterPromoOffers = new ArrayList[]{new ArrayList<>()};
        final ArrayList<MasterPromoOffer>[] finalPromoOffers = new ArrayList[]{new ArrayList<>()};
        ((DealarBoardActivity) requireActivity()).setOfferMenuRefreshListener(() -> {
            db.open();
            masterPromoOffers[0] = db.getmaterPromoOffers();
            if (!masterPromoOffers[0].isEmpty()) {
                finalPromoOffers[0] = new ArrayList<>();
                for (int k = 0; k < masterPromoOffers[0].size(); k++) {
                    if (CommonFunctions.isTablet(requireActivity())) {
                        String path = CommonString.getPromosFolder(getContext()) + masterPromoOffers[0].get(k).getPromoOfferTab();
                        File dir = new File(path);
                        if (dir.exists()) {
                            finalPromoOffers[0].add(masterPromoOffers[0].get(k));
                        }
                    } else {
                        String path = CommonString.getPromosFolder(getContext()) + masterPromoOffers[0].get(k).getPromoOfferMobile();
                        File dir = new File(path);
                        if (dir.exists()) {
                            finalPromoOffers[0].add(masterPromoOffers[0].get(k));
                        }
                    }
                }
            }


            ///add data for temp
            if (finalPromoOffers[0].isEmpty()) {
                MasterPromoOffer sb = new MasterPromoOffer();
                sb.setPromoId("No Data");
                sb.setPromoPath("");
                sb.setPromoOfferTab("");
                sb.setPromoOfferMobile("");
                sb.setDescription("");
                sb.setStartDate("");
                sb.setEndDate("");
                finalPromoOffers[0].add(sb);

            }


            if (finalPromoOffers[0].size() > 0) {
                myPager = new MyPager(view.getContext(), finalPromoOffers[0]);
                viewPager.setAdapter(myPager);
                circleIndicator = view.findViewById(R.id.circle);
                circleIndicator.setViewPager(viewPager);
            }
        });

        db.open();
        masterPromoOffers[0] = db.getmaterPromoOffers();
        if (!masterPromoOffers[0].isEmpty()) {
            finalPromoOffers[0] = new ArrayList<>();
            for (int k = 0; k < masterPromoOffers[0].size(); k++) {
                if (CommonFunctions.isTablet(requireContext())) {
                    String path = CommonString.getPromosFolder(getContext()) + masterPromoOffers[0].get(k).getPromoOfferTab();
                    File dir = new File(path);
                    if (dir.exists()) {
                        finalPromoOffers[0].add(masterPromoOffers[0].get(k));
                    }
                } else {
                    String path = CommonString.getPromosFolder(getContext()) + masterPromoOffers[0].get(k).getPromoOfferMobile();
                    File dir = new File(path);
                    if (dir.exists()) {
                        finalPromoOffers[0].add(masterPromoOffers[0].get(k));
                    }
                }
            }
        }


        ///add data for temp
        if (finalPromoOffers[0].isEmpty()) {
            MasterPromoOffer sb = new MasterPromoOffer();
            sb.setPromoId("No Data");
            sb.setPromoPath("");
            sb.setPromoOfferTab("");
            sb.setPromoOfferMobile("");
            sb.setDescription("");
            sb.setStartDate("");
            sb.setEndDate("");
            finalPromoOffers[0].add(sb);

        }


        if (finalPromoOffers[0].size() > 0) {
            myPager = new MyPager(view.getContext(), finalPromoOffers[0]);
            viewPager.setAdapter(myPager);
            circleIndicator = view.findViewById(R.id.circle);
            circleIndicator.setViewPager(viewPager);
        }

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (viewPager != null) {
                    viewPager.post(() -> {
                        if (!finalPromoOffers[0].isEmpty())
                            viewPager.setCurrentItem((viewPager.getCurrentItem() + 1) % finalPromoOffers[0].size());
                    });

                }
            }
        };

        timer = new Timer();
        timer.schedule(timerTask, 3000, 3000);
        return view;
    }

    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }
}
