package org.aplas.myshop;

import android.os.Build;

import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;

import org.aplas.myshop.model.User;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class TestC2MyShop011 extends ViewTest {
    ActivityScenario<LoginActivity> scenario;
    private String packageName = "org.aplas";
    private String targetDevice = "9";
    private int minSDK = 21;
    private String actName = "LoginActivity";
    private String layoutName = "activity_login";
    private String backwardComp = "AppCompatActivity";
    //private String packName;
    //ResourceTest rsc;

    @Before
    public void initTest() {
        scenario = ActivityScenario.launch(LoginActivity.class);
        scenario.moveToState(Lifecycle.State.CREATED);
    }


    @Test
    public void check_01_AppName() { //Check Project Name (Should be MyData)
        //ActivityScenario<MainActivity> main = launchMainApp();
        scenario.onActivity(activity -> {
            assertEquals("Application Name is Wrong", appName.toLowerCase(), getAppName(activity.getPackageName()));
        });
    }

    @Test
    public void check_02_PackageName() { //Check Company Domain (Should be org.aplas.android)
        scenario.onActivity(activity -> {
            String packName = packageName+"."+appName.toLowerCase();
            assertEquals("Package Name is Wrong", packName, activity.getPackageName());
        });
    }

    @Test
    public void check_03_TargetDevice() { //Check Target Device (Should be 6.0.1)
        scenario.onActivity(activity -> {
            assertEquals("Target Device is Wrong",targetDevice, Build.VERSION.RELEASE);
        });
    }


    @Test
    public void check_04_MinimumSDK() { //Check Minimum SDK (Should be 28)
        scenario.onActivity(activity -> {
            assertEquals("Minimum SDK Version is Wrong",minSDK,activity.getApplicationInfo().minSdkVersion);
        });
    }

    @Test
    public void check_05_ActivityName() { //Check Activity Name (Should be MyActivity)
        scenario.onActivity(activity -> {
            assertEquals("Activity Name is Wrong", actName, activity.getClass().getSimpleName());
        });
    }

    @Test
    public void check_06_LayoutName() { //Check Layout Name (Should be activity_layout)
        scenario.onActivity(activity -> {
            int resId = activity.getResources().getIdentifier(layoutName, "layout", activity.getPackageName());
            assertNotEquals("Layout Name is Wrong", 0, resId);
        });
    }

    @Test
    public void check_07_ActivityParent() { //Check Backward Compatibility (Should be AppCompatActivity)
        scenario.onActivity(activity -> {
            assertEquals("Activity Parent is Wrong", backwardComp, activity.getClass().getSuperclass().getSimpleName());
        });
    }

    @Test
    public void check_08_String_Resource() {
        //string check
        scenario.onActivity(activity -> {
            org.aplas.myshop.ResourceTest rsc = new org.aplas.myshop.ResourceTest(activity.getResources());
            rsc.testStringResource("app_name","MyShop");
            rsc.testStringResource("hint_name","Name");
            rsc.testStringResource("hint_username","Username");
            rsc.testStringResource("hint_password","Password");
            rsc.testStringResource("hint_confirm_password","Confirm Password");
            rsc.testStringResource("text_login","Login");
            rsc.testStringResource("text_register","Register");
            rsc.testStringResource("error_message_username","Enter Username");
            rsc.testStringResource("error_message_password","Enter Password");
            rsc.testStringResource("error_message_role","Select Your Role");
            rsc.testStringResource("success_message","Registration Successful");
            rsc.testStringResource("text_not_member","No account yet? Create one");
            rsc.testStringResource("text_already_member","Already a member? Login");
            rsc.testStringResource("error_username_exists","Username Already Exists");
            rsc.testStringResource("error_password_match","Password Does Not Matches");
            rsc.testStringResource("error_valid_email_password","Wrong Email or Password");
            rsc.testStringResource("radio_seller","Seller");
            rsc.testStringResource("radio_buyer","Buyer");
            rsc.testStringResource("hint_product_name","Product Name");
            rsc.testStringResource("hint_product_price","Product Price");
            rsc.testStringResource("hint_product_buyer","Product Buyer");
            rsc.testStringResource("hint_product_quantity","Quantity");
            rsc.testStringResource("text_add_product","Add Product");
            rsc.testStringResource("text_remove_product","Remove Product");
            rsc.testStringResource("text_add_to_cart","Add To Cart");
            rsc.testStringResource("text_back","Back");
            rsc.testStringResource("error_message_product_name","Enter Product Name");
            rsc.testStringResource("error_message_product_price","Enter Product Price");
            rsc.testStringResource("product_success_message","Add Product Successful");
            rsc.testStringResource("error_product_exists","Product Already Exists");
            String[] data = {"---You Want to Be A?---", "Seller", "Buyer"};
            rsc.testStringArrayResource("roles",data);

        });
    }

    @Test
    public void check_09_Color_Resources() {
        scenario.onActivity(activity -> {
            org.aplas.myshop.ResourceTest rsc = new org.aplas.myshop.ResourceTest(activity.getResources());
            rsc.testColorResource(activity, "black", "#000000");
            rsc.testColorResource(activity, "white", "#FFFFFF");
            rsc.testColorResource(activity, "colorPrimary", "#51d8c7");
            rsc.testColorResource(activity, "colorPrimaryDark", "#51d8c7");
            rsc.testColorResource(activity, "colorAccent", "#FFFFFF");
            rsc.testColorResource(activity, "colorBackground", "#001a33");
            rsc.testColorResource(activity, "colorText", "#FFFFFF");
            rsc.testColorResource(activity, "colorTextHint", "#51d8c7");
        });

    }

    @Test
    public void check_10_Image_Resource(){
        scenario.onActivity(activity -> {
            org.aplas.myshop.ResourceTest rsc = new org.aplas.myshop.ResourceTest(activity.getResources());
            rsc.testImgResource("myshop");
        });

    }

    @Test
    public void check_11_ImageView(){
        scenario.onActivity(activity -> {
            testViewExist("imgLogo","ImageView", activity);
        });
    }

    @Test
    public void check_12_EditText(){
        scenario.onActivity(activity -> {
            testViewExist("textInputEditTextUsername","EditText", activity);
            testViewExist("textInputEditTextPassword","EditText", activity);
        });
    }

    @Test
    public void check_13_Button(){
        scenario.onActivity(activity -> {
            testViewExist("appCompatButtonLogin","Button", activity);
        });
    }

    @Test
    public void check_14_TextView(){
        scenario.onActivity(activity -> {
            testViewExist("textViewLinkRegister","TextView", activity);
        });
    }

    @Test
    public void check_15_User_Class_Properties() {
        String packname = "org.aplas.myshop.model";
        String cname = "User";
        try {
            Class<?> c = Class.forName(packname+"."+cname);
            Constructor<?> ctor = c.getConstructor();
            Object obj = ctor.newInstance();
            assertEquals("Class "+cname+" should be in 'model' directory", packname, obj.getClass().getPackage().getName());
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            failTest("Class " + cname + " is not defined ("+ e.toString() +")");
        }
    }

    private String getAppName(String packName) {
        String[] list = packName.split("\\.");
        String res = list[list.length-1];
        return res;
    }

}
