package com.itachi1706.cheesecakeutilities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.github.lzyzsd.circleprogress.ArcProgress;
import com.itachi1706.cheesecakeutilities.RecyclerAdapters.StringRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ORDActivity extends BaseActivity {

    RecyclerView recyclerView;
    TextView ordCounter, ordDaysLabel, ordProgress;
    ArcProgress progressBar;
    int animationDuration = 2500; // ms
    long ordDays, ptpDays, popDays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ord);

        recyclerView = (RecyclerView) findViewById(R.id.ord_recycler_view);
        ordDaysLabel = (TextView) findViewById(R.id.ord_days_counter);
        ordCounter = (TextView) findViewById(R.id.ord_counter);
        ordProgress = (TextView) findViewById(R.id.ord_precentage);
        progressBar = (ArcProgress)  findViewById(R.id.ord_progressbar);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (recyclerView != null) {
            recyclerView.setHasFixedSize(true);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());

            // Set up layout
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
            long ord = sp.getLong(ORDSettingsActivity.SP_ORD, 0);
            long ptp = sp.getLong(ORDSettingsActivity.SP_PTP, 0);
            long pop = sp.getLong(ORDSettingsActivity.SP_POP, 0);
            long enlist = sp.getLong(ORDSettingsActivity.SP_ENLIST, 0);
            long currentTime = System.currentTimeMillis();
            List<String> menuItems = new ArrayList<>();
            if (ptp != 0) {
                // Non Enhanced Batch
                if (currentTime > ptp) {
                    // PTP LOH
                    menuItems.add("PTP Phase Ended");
                } else {
                    long duration = ptp - currentTime;
                    ptpDays = TimeUnit.MILLISECONDS.toDays(duration) + 1;
                    menuItems.add(getResources().getQuantityString(R.plurals.ord_days_countdown, (int) ptpDays, ptpDays, "Start of BMT Phase"));
                }
            }

            if (pop != 0) {
                if (currentTime > pop) {
                    // POP LOH
                    menuItems.add("POP LOH");
                } else {
                    long duration = pop - currentTime;
                    popDays = TimeUnit.MILLISECONDS.toDays(duration) + 1;
                    menuItems.add(getResources().getQuantityString(R.plurals.ord_days_countdown, (int) popDays, popDays, "POP"));
                }
            } else {
                menuItems.add("POP Date not defined. Please define in settings");
            }

            if (ord != 0) {
                if (currentTime > ord) {
                    // ORD LOH
                    ordDaysLabel.setText("LOH");
                    ordCounter.setText("ORD");
                    ordProgress.setText("100% Completed");
                    progressBar.setProgress(100);
                } else {
                    long duration = ord - currentTime;
                    ordDays = TimeUnit.MILLISECONDS.toDays(duration) + 1;
                    ordDaysLabel.setText(getResources().getQuantityString(R.plurals.ord_days, (int) ordDays));
                    ordCounter.setText(ordDays + "");

                    double difference = ((TimeUnit.MILLISECONDS.toDays(currentTime-enlist)) / (double)(TimeUnit.MILLISECONDS.toDays(ord - enlist))) * 100.0;
                    ordProgress.setText((Math.round(difference * 100.0)/100.0) + "% completed");
                    progressBar.setProgress((int)difference);
                }
            } else {
                menuItems.add("ORD Date not defined. Please define in settings");
            }


            StringRecyclerAdapter adapter = new StringRecyclerAdapter(menuItems);
            recyclerView.setAdapter(adapter);
        }
    }

    @Override
    String getHelpDescription() {
        return "ORD Calculator (WIP)";
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.modules_ord, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.modify_settings: startActivity(new Intent(getApplicationContext(), ORDSettingsActivity.class)); return true;
            default: return super.onOptionsItemSelected(item);
        }
    }
}

@@ -586,24 +586,51 @@
 .ArrayList;%0A
+import java.util.Calendar;%0A
 import java.
@@ -844,48 +844,8 @@
 ar;%0A
-    int animationDuration = 2500; // ms%0A
     
@@ -4384,16 +4384,261 @@
 erence);
+%0A%0A                    int weekends = calculateWeekends();%0A                    int weekdays = calculateWeekdays(weekends);%0A                    menuItems.add(weekdays + %22 Working Day(s)%22);%0A                    menuItems.add(weekends + %22 Weekends%22);
 %0A       
@@ -4904,32 +4904,652 @@
        %7D%0A    %7D%0A%0A
+    private int calculateWeekends() %7B%0A        if (ordDays == 0) return 0;%0A        int weekends = 0;%0A        Calendar cal = Calendar.getInstance();%0A        for (int i = 0; i %3C ordDays; i++) %7B%0A            if (i != 0) %7B%0A                cal.add(Calendar.DAY_OF_WEEK, 1);%0A            %7D%0A            if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY %7C%7C cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) %7B%0A                weekends++;%0A            %7D%0A        %7D%0A        return weekends;%0A    %7D%0A%0A    private int calculateWeekdays(int weekends) %7B%0A        if (ordDays == 0) return 0;%0A        return (int)ordDays - weekends;%0A    %7D%0A%0A
     @Override%0A  

Add Weekdays and Weekends calculations