package com.me.shots.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.me.shots.PptWebViewActivity;
import com.me.shots.R;
import com.me.shots.Utils.ModulesActivity;

import me.itangqi.waveloadingview.WaveLoadingView;

/**
 * Created by HP-USER on 11-12-2017.
 */

public class ModuleAdapter extends BaseAdapter {
    int modulecount=0;
    String[] modulenames={"machine","machine","machine","machine","machine"};
    Context context=null;
    String[] courselinks=null;
    public ModuleAdapter(int modulecount, String[] modulenames, Context context,String[] courselinks){
        this.modulecount=modulecount;
        this.modulenames=modulenames;
        this.courselinks=courselinks;
        Log.e("mytag", "ModuleAdapter: "+modulecount );
        this.context=context;
    }

    @Override
    public int getCount() {
        int count=2*(modulecount/3)+(modulecount%3);
        Log.e("mytag", "getCount: "+count );
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view= LayoutInflater.from(context).inflate(R.layout.even_module_views,parent,false);


        WaveLoadingView leftmWaveLoadingView = (WaveLoadingView) view.findViewById(R.id.leftWaveLoadingView);
        WaveLoadingView centermWaveLoadingView = (WaveLoadingView) view.findViewById(R.id.centerWaveLoadingView);
        WaveLoadingView rightmWaveLoadingView = (WaveLoadingView) view.findViewById(R.id.rightWaveLoadingView);

        waveviews(leftmWaveLoadingView);
        waveviews(centermWaveLoadingView);
        waveviews(rightmWaveLoadingView);
        Log.e("mytag", "position: "+position );

        if(position%2==0)
        {
            final int moduleIndex=getEvenViews(position);

            String name[]=modulenames[moduleIndex].split(".pptx");
            Log.e("mytag", "getView: "+moduleIndex+"==========="+modulenames[moduleIndex] );

            centermWaveLoadingView.setCenterTitle(name[0]);
            centermWaveLoadingView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,"center"+moduleIndex,Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context,PptWebViewActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("modulelink",courselinks[moduleIndex]);
                    Log.e("mytag1", "onClick:----------- "+courselinks[moduleIndex] );
                    context.startActivity(intent);
                }
            });
            RelativeLayout left= (RelativeLayout) view.findViewById(R.id.left_layout);
            left.setVisibility(View.INVISIBLE);

            RelativeLayout right= (RelativeLayout) view.findViewById(R.id.right_layout);
            right.setVisibility(View.INVISIBLE);
        }
        else {
            final int moduleIndex=getEvenViews(position);
            if(moduleIndex+2==modulecount){
                String name[]=modulenames[moduleIndex+1].split(".pptx");
                Log.e("mytag", "getView: "+moduleIndex+"==========="+modulenames[moduleIndex] );

                centermWaveLoadingView.setCenterTitle(name[0]);
                centermWaveLoadingView.setWaveColor(Color.parseColor("#D47FFF"));
                centermWaveLoadingView.setBorderColor(Color.parseColor("#D47FFF"));
                centermWaveLoadingView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"center"+moduleIndex,Toast.LENGTH_SHORT).show();
                        Log.e("mytag1", "onClick:----------- "+courselinks[moduleIndex] );

                        Intent intent = new Intent(context,PptWebViewActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("modulelink",courselinks[moduleIndex]);
                        context.startActivity(intent);

                    }
                });
                RelativeLayout left= (RelativeLayout) view.findViewById(R.id.left_layout);
                left.setVisibility(View.INVISIBLE);

                RelativeLayout right= (RelativeLayout) view.findViewById(R.id.right_layout);
                right.setVisibility(View.INVISIBLE);

            }
            else
            {
                RelativeLayout center= (RelativeLayout) view.findViewById(R.id.center_layout);
                center.setVisibility(View.INVISIBLE);


                leftmWaveLoadingView.setWaveColor(Color.parseColor("#AAFF552A"));
                leftmWaveLoadingView.setBorderColor(Color.parseColor("#AAFF551A"));
                leftmWaveLoadingView.setProgressValue(60);
                String name[]=modulenames[moduleIndex+1].split(".pptx");
                leftmWaveLoadingView.setCenterTitle(name[0]);
                String name1[]=modulenames[moduleIndex+2].split(".pptx");
                leftmWaveLoadingView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"Left"+moduleIndex,Toast.LENGTH_SHORT).show();
                        Log.e("mytag1", "onClick:----------- "+courselinks[moduleIndex+1] );

                        Intent intent = new Intent(context,PptWebViewActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("modulelink",courselinks[moduleIndex+1]);
                        context.startActivity(intent);

                    }
                });

                rightmWaveLoadingView.setWaveColor(Color.parseColor("#55FF2A"));
                rightmWaveLoadingView.setBorderColor(Color.parseColor("#55FF1A"));
                rightmWaveLoadingView.setProgressValue(60);
                rightmWaveLoadingView.setCenterTitle(name1[0]);
                rightmWaveLoadingView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"Right"+moduleIndex,Toast.LENGTH_SHORT).show();
                        Log.e("mytag1", "onClick:----------- "+courselinks[moduleIndex+2] );

                        Intent intent = new Intent(context,PptWebViewActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("modulelink",courselinks[moduleIndex+2]);
                        context.startActivity(intent);

                    }
                });
            }


        }


        return view;
    }


    private void waveviews(WaveLoadingView mWaveLoadingView){
        mWaveLoadingView.setShapeType(WaveLoadingView.ShapeType.CIRCLE);
        //mWaveLoadingView.setTopTitle("Top Title");
        mWaveLoadingView.setCenterTitleColor(Color.GRAY);
        mWaveLoadingView.setBottomTitleSize(18);
        mWaveLoadingView.setProgressValue(60);
        mWaveLoadingView.setBorderWidth(7);
        mWaveLoadingView.setAmplitudeRatio(60);
        mWaveLoadingView.setWaveColor(Color.parseColor("#2AFFD4"));
        mWaveLoadingView.setBorderColor(Color.parseColor("#2BFFD4"));
        mWaveLoadingView.setTopTitleStrokeColor(Color.BLUE);
        mWaveLoadingView.setTopTitleStrokeWidth(2);
        mWaveLoadingView.setAnimDuration(2000);
        // mWaveLoadingView.pauseAnimation();
        // mWaveLoadingView.resumeAnimation();
        //   mWaveLoadingView.cancelAnimation();
        mWaveLoadingView.startAnimation();

    }

    private int getEvenViews(int mcount){
        return (mcount/2)*3;
    }

}