// Copyright 2015 The Project Buendia Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distrib-
// uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY KIND, either express or implied.  See the License for
// specific language governing permissions and limitations under the License.

package org.projectbuendia.client.ui.dialogs;

import android.app.Activity;
import android.support.annotation.IdRes;
import android.support.test.annotation.UiThreadTest;
import android.support.test.runner.AndroidJUnit4;

import androidx.test.filters.SmallTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.projectbuendia.client.R;
import org.projectbuendia.client.ui.FunctionalTestCase;
import org.projectbuendia.client.ui.chart.PatientChartActivity;

import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static junit.framework.TestCase.assertTrue;

/** Tests for adding a new patient. */
@RunWith(AndroidJUnit4.class)
@SmallTest
public class EditPatientDialogFragmentTest extends FunctionalTestCase {

    /**
     * Tests adding a new patient;
     * Tests adding a location to the patient;
     * Test symptom date;
     * Test last observation date;
     * Test admission date;
     */
    @Test
    @UiThreadTest
    public void testNewPatient() {
        // Create the patient
        String id = inUserLoginGoToDemoPatientChart();

        // Assign a location to the patient
        click(viewWithId(R.id.attribute_location));
        screenshot("After Location Dialog Shown");
        click(viewWithText(LOCATION_NAME));
        screenshot("After Location Selected");

        pressBack();

        // The new patient should be visible in the list for their location
        click(viewWithText(LOCATION_NAME));
        screenshot("In " + LOCATION_NAME);
        inPatientListClickPatientWithId(id);
        screenshot("After Patient Clicked");

        // The symptom onset date should not be assigned a default value.
        expectVisible(viewThat(
            hasAncestorThat(withId(R.id.attribute_symptoms_onset_days)),
            hasText("–")));

        // The admission date should be visible right after adding a patient.
        // Flaky because of potential periodic syncs.
        expectVisibleWithin(399999, viewThat(
            hasAncestorThat(withId(R.id.attribute_admission_days)),
            hasText("Day 1")));
    }

    @Test
    @UiThreadTest
    public void testPatientCreation() throws Throwable {
        inUserLoginGoToPatientCreation();
        screenshot("Test Start");
        String id = generateId();
        // Populate the patient info
        screenshot("Before Patient Populated");
        String given = "Giv" + id.substring(id.length() - 2);
        String family = "Fam" + id.substring(id.length() - 4, id.length() - 2);
        type(id, viewWithId(R.id.patient_id));
        type(given, viewWithId(R.id.patient_given_name));
        type(family, viewWithId(R.id.patient_family_name));
        type(4, viewWithId(R.id.patient_age_years));
        type(2, viewWithId(R.id.patient_age_months));
        boolean female = Integer.parseInt(id) % 2 == 0;
        @IdRes int sexButton = female ? R.id.patient_sex_female : R.id.patient_sex_male;
        click(viewWithId(sexButton));
        screenshot("After Patient Populated");
        click(viewWithText("OK"));
        waitForProgressFragment();
        screenshot("On Patient Chart");
        Activity activity = getCurrentActivity();

        // Now read off the patient info and check that it's all there.
        // It should all be in the action bar.
        assertTrue("Expected PatientChartActivity, got something else",
                activity instanceof PatientChartActivity);
        expectVisible(viewThat(hasTextContaining(id + ".")));
        expectVisible(viewThat(hasTextContaining(given)));
        expectVisible(viewThat(hasTextContaining(family)));
        if (female) {
            expectVisible(viewThat(hasTextContaining("F,")));
        } else {
            expectVisible(viewThat(hasTextContaining("M,")));
        }
        expectVisible(viewThat(hasTextContaining((4 * 12 + 2) + " mo")));
    }
}

@@ -1065,72 +1065,8 @@
 y;%0A%0A
-import static android.support.test.espresso.Espresso.pressBack;%0A
 impo
@@ -1395,24 +1395,23 @@
 * Tests 
-adding a
+initial
  locatio
@@ -1404,34 +1404,34 @@
 nitial location 
-t
 o
+f
  the patient;%0A  
@@ -1513,24 +1513,71 @@
 ssion date;%0A
+     * Tests adding a location to the patient;%0A
      */%0A    
@@ -1733,588 +1733,282 @@
     
-// Assign a location to the patient%0A        click(viewWithId(R.id.attribute_location));%0A        screenshot(%22After Location Dialog Shown%22);%0A        click(viewWithText(LOCATION_NAME));%0A        screenshot(%22After Location Selected%22);%0A%0A        pressBack();%0A%0A        // The new patient should be visible in the list for their location%0A        click(viewWithText(LOCATION_NAME));%0A        screenshot(%22In %22 + LOCATION_NAME);%0A        inPatientListClickPatientWithId(id);%0A        screenshot(%22After Patient Clicked%22);%0A%0A        // The symptom onset date should not be assigned a default value.
+expectVisibleSoon(viewThat(hasTextContaining(%22Triage%22)));%0A%0A        // The symptom onset date should not be assigned a default value.%0A        expectVisible(viewThat(%0A                hasAncestorThat(withId(R.id.attribute_symptoms_onset_days)),%0A                hasText(%22%E2%80%93%22)));%0A
 %0A   
@@ -2017,33 +2017,47 @@
    expectVisible
-(
+Within(399999, 
 viewThat(%0A      
@@ -2042,32 +2042,36 @@
 9999, viewThat(%0A
+    
             hasA
@@ -2100,38 +2100,33 @@
 d.attribute_
-symptoms_onset
+admission
 _days)),%0A   
@@ -2126,32 +2126,36 @@
 )),%0A            
+    
 hasText(%22%E2%80%93%22)));%0A
@@ -2139,33 +2139,37 @@
        hasText(%22
-%E2%80%93
+Day 1
 %22)));%0A%0A        /
@@ -2174,73 +2174,40 @@
  // 
-The admission date should be visible right after adding a
+Assign a location to the
  patient
 .%0A  
@@ -2202,17 +2202,16 @@
  patient
-.
 %0A       
@@ -2215,195 +2215,261 @@
     
-// Flaky because of potential periodic syncs.%0A        expectVisibleWithin(399999, viewThat(%0A            hasAncestorThat(withId(R.id.attribute_admission_days)),%0A            hasText(%22Day 1%22
+click(viewWithId(R.id.attribute_location));%0A        screenshot(%22After Location Dialog Shown%22);%0A        click(viewWithText(LOCATION_NAME));%0A        screenshot(%22After Location Selected%22);%0A%0A        expectVisibleSoon(viewThat(hasTextContaining(LOCATION_NAME
 )));

update EditPatientDialogFragmentTest testNewPatient to be less fragile